【问题标题】:How can I create an instance which is defined in \App_Code in Vs 2019?如何在 Vs 2019 中创建在 \\App_Code 中定义的实例?
【发布时间】:2023-02-06 15:14:19
【问题描述】:

我用VS 2019创建了一个项目,项目结构大家可以看Image。

我在位于\App_Code 文件夹下的HandelRecor.cs 文件中定义了一个名为HandelRecord 的类,我希望在Default.aspx.cs 中使用它。

但是HandelRecord aHandelRecord = new HandelRecord()无法编译,请问如何解决?

默认.aspx.cs

using System;

namespace TempDB
{
    public partial class Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                setLabel();
            }
        }

        private void setLabel()
        {          
            HandelRecord aHandelRecord = new HandelRecord();    //Can't be compiled
        }
    }
}

处理记录.cs

using System;
...

namespace TempDB.App_Code
{
    public class HandelRecord
    {
        ...
    }
}

图片 1

添加内容:

致:Albert D. Kallal,谢谢!

将 .cs 文件放置到 \App_Code 是一种不好的方法吗?你可以看到图像A,我必须设置编译.cs 文件的选项。

如果我将 .cs 文件放置到项目的根文件夹中,则 Vs 2019 设置编译.cs 文件自动选项,您可以看到图像 B。

图片A

图片B

【问题讨论】:

    标签: asp.net visual-studio


    【解决方案1】:

    右键单击 app_code 中的 .cs 文件。检查构建选项并将其更改为“编译”

    例如这个:

    【讨论】:

    • 谢谢!但是在我尝试你的方法后我得到了同样的错误。
    • E:ASP.NET_TempDBTempDBDefault.aspx.cs(23,13,23,25): error CS0246: The type or namespace name 'HandelRecord' could not be found (are you missing a using directive or an assembly reference?)
    • 这个对我有用。但是,在您的情况下,这可能是一个网站而不是 Web 应用程序。重建一切只是为了确定
    • 谢谢!请您看看我在问题中添加的内容好吗?
    • 向 app_code 添加(添加)新类的标准公平。我不是 100% 确定,但我确实(曾经)想过,右键单击 app_folder-> 添加新项目-> 类。我似乎记得这会将属性设置为“编译”。然而,似乎并没有。所以,只要记住那个额外的步骤。请注意,如果这是一个网站“应用程序”,则“请勿复制”是可以的。但是,如果这是一个网站,则需要非常肯定地复制到输出。
    猜你喜欢
    • 2021-04-21
    • 2021-05-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-10-15
    • 2012-05-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多