无法从代码隐藏调用App_Code文件类(Unable to call App_Code class

2019-07-17 22:55发布

我有一个类的文件,该文件在“App_Code文件”文件夹中。 我能够在“ASPX”的文件,但不使用这个从代码隐藏文件。 如何使其代码隐藏可见?

注:这是单声道ASP.Net,我直接写的类,而不是使用IDE编译它们

我的文件:

aspx文件(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>

后面的代码(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() {}
}

我试着给它一个名称空间,但这并不解决问题。

Answer 1:

改变你的类的Build Action进行编译。



文章来源: Unable to call App_Code class from a code-behind