Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
562 views
in Technique[技术] by (71.8m points)

asp.net - Unable to call App_Code class from a code-behind

I have a class in a file that's in the "App_Code" folder. I'm able to use this in an "aspx" file but not from a code-behind file. How do I make it visible to a code-behind?

NOTE: This is ASP.Net on Mono and I'm writing the classes directly, not using an IDE to compile them

My files:

ASPX FILE (testappcode.aspx)

<%@ Page language="c#" src="TestAppCode.aspx.cs" Inherits="TestAppCode.TestAppCode" AutoEventWireup="true" %>
<html>
  <head>
    <title>Test App_Code Folder</title>
  </head>
  <body>
    <form id="contactForm" runat="server">
    <asp:TextBox id="Name" runat="server" ></asp:TextBox>
    <asp:TextBox id="Age" runat="server" ></asp:TextBox>
    <asp:Button ID="Submit" runat="server" Text="Submit" onclick="SubmitForm" />
    </form>
  </body>
<html>

CODE BEHIND (TestAppCode.aspx.cs)

using System;
using System.Web.UI.WebControls;

namespace TestAppCode
{
    public class TestAppCode : System.Web.UI.Page
    {
    protected void SubmitForm(object sender, EventArgs e)
    {
        //It fails here with the error: CS0246: The type or namespace name
        //`MyAppCodeClass' could not be found. Are you missing a using
        //directive or an assembly reference?
        MyAppCodeClass m = new MyAppCodeClass();
    }
    }
}

APP_CODE CLASS (App_Code/MyAppCodeClass.cs)

public class MyAppCodeClass
{
         public MyAppCodeClass() {}
}

I tried giving it a namespace, but that doesn't fix the issue.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Change your class' Build Action to Compile.

enter image description here


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...