本文介绍了无法从代码隐藏调用 App_Code 类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在App_Code"文件夹中的文件中有一个类.我可以在aspx"文件中使用它,但不能从代码隐藏文件中使用.如何使其对代码隐藏可见?

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?

注意:这是 Mono 上的 ASP.Net,我直接编写类,而不是使用 IDE 编译它们

我的文件:

<%@ 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() {}
}

我尝试给它一个命名空间,但这并不能解决问题.

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

推荐答案

将你的类的 Build Action 更改为 Compile.

Change your class' Build Action to Compile.

这篇关于无法从代码隐藏调用 App_Code 类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-03 13:32