ASP.NET 无法引用类

发布于 2024-10-14 12:21:08 字数 2068 浏览 10 评论 0原文

我已将 CS 文件移出 App_Data 文件夹,因为我在重复构建时遇到错误,一个在编译时构建,一个在运行时构建,所以我被告知将它们移出该文件夹。

两个文件,Default.aspx.cs

namespace CrystalTech
{
    public partial class newVersion_Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            LoginInitialiser loginData = new LoginInitialiser();
            loginData.loadData();
        }
    }
}

和 Security.cs

namespace CrystalTech
{
    // Handles all our login logic
    public class LoginInitialiser
    {
        public bool isLoggedIn { get; private set; }

        public LoginInitialiser()
        {
            this.isLoggedIn = false;
        }

        // Fetches the users data
        public void loadData()
        {
            if (HttpContext.Current.Session["loggedIn"] != null)
            {
                this.isLoggedIn = (bool)HttpContext.Current.Session["loggedIn"];
            }

            // Fetch the data if we are logged in
            if (this.isLoggedIn)
            {

            }
            // Otherwise redirect
            else
            {
                HttpContext.Current.Response.Redirect("../logins/index.asp?action=dn&r=" + CommonFunctions.GetCurrentPageName());
            }
        }
    }

    // Holds users information
    public class User
    {
        public int ID { get; private set; }
        public string username { get; private set; }
        public Company company { get; private set; }
        public string title { get; private set; }
        public string forenames { get; private set; }
        public string surnames { get; private set; }
        public string email { get; private set; }
    }

    // Holds company information
    public class Company
    {
        public int ID { get; private set; }
        public string name { get; private set; }
        public string telephone { get; private set; }
    }
}

为什么 Default.aspx.cs 会抛出:

找不到类型或命名空间名称“LoginInitialiser”(是否缺少 using 指令或程序集引用?)

I've moved the CS files out the App_Data folder because I get errors with duplicate builds, one builds at compile time one at run time so I was told to move them out that folder.

Two files, Default.aspx.cs

namespace CrystalTech
{
    public partial class newVersion_Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            LoginInitialiser loginData = new LoginInitialiser();
            loginData.loadData();
        }
    }
}

And Security.cs

namespace CrystalTech
{
    // Handles all our login logic
    public class LoginInitialiser
    {
        public bool isLoggedIn { get; private set; }

        public LoginInitialiser()
        {
            this.isLoggedIn = false;
        }

        // Fetches the users data
        public void loadData()
        {
            if (HttpContext.Current.Session["loggedIn"] != null)
            {
                this.isLoggedIn = (bool)HttpContext.Current.Session["loggedIn"];
            }

            // Fetch the data if we are logged in
            if (this.isLoggedIn)
            {

            }
            // Otherwise redirect
            else
            {
                HttpContext.Current.Response.Redirect("../logins/index.asp?action=dn&r=" + CommonFunctions.GetCurrentPageName());
            }
        }
    }

    // Holds users information
    public class User
    {
        public int ID { get; private set; }
        public string username { get; private set; }
        public Company company { get; private set; }
        public string title { get; private set; }
        public string forenames { get; private set; }
        public string surnames { get; private set; }
        public string email { get; private set; }
    }

    // Holds company information
    public class Company
    {
        public int ID { get; private set; }
        public string name { get; private set; }
        public string telephone { get; private set; }
    }
}

Why does Default.aspx.cs throw:

The type or namespace name 'LoginInitialiser' could not be found (are you missing a using directive or an assembly reference?)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

橘寄 2024-10-21 12:21:08

如果您希望您的类位于 App_Code 之外的其他位置,请在您的解决方案中添加一个“类库”项目并将该类放在那里。不要忘记在您的网络项目中引用该项目。

If you want your classes to live somewhere other than App_Code, add a "Class Library" project to your solution and put that class there. Don't forget to reference that project in your webproject.

不寐倦长更 2024-10-21 12:21:08

仅编译 App_Code 目录中的代码 - 因此,除非您的文件位于该目录(或子目录)中,否则它们不会被拾取

重复构建是什么意思?根据项目设置,实际构建可以通过首次访问站点或编译本身来触发。也可以做一半。事先进行一些准备(构建/发布)并在第一次站点访问时进行实际编译

Only code in the App_Code directory is compiled - So unless your files are in there (or a subdirectory), they won't be picked up

And what do you mean by duplicate builds? Depending on project settings, the actual build can be triggered either by the first visit to the site or by the compile itself. It's also possible to do half-and-half. with a little preparation beforehand on (build/publish) and the actual compile on the first site visit

时光倒影 2024-10-21 12:21:08

代码文件(与 aspx 文件相关的除外)放在 App_Code 文件夹中只是为了确保它们的编译(这是 ASP.net 强加的要求,现在我从注释中知道它仅适用于 < code>web site 项目而不是 web application 项目)。如果它们在其他地方,则不会被编译。检查一下,您将找不到 App_Code 文件夹之外的代码文件的 Build Action

The code files (except the ones related to aspx files) are put in the App_Code folder only to ensure their compilation (This is requirement imposed by ASP.net and now I know from comments it is only for web site projects and not web application projects). If they are anywhere else they are not compiled. Check and you will not find Build Action for the code files outside App_Code folder.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文