简体   繁体   English

ASP.NET无法引用类

[英]ASP.NET can't reference class

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. 我将CS文件移出了App_Data文件夹,因为在重复的构建过程中出现错误,在编译时构建了一个,在运行时构建了一个,因此被告知要将它们移出该文件夹。

Two files, Default.aspx.cs 两个文件,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 和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: 为什么Default.aspx.cs抛出:

The type or namespace name 'LoginInitialiser' could not be found (are you missing a using directive or an assembly reference?) 找不到类型或名称空间名称“ LoginInitialiser”(您是否缺少using指令或程序集引用?)

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. 如果您希望您的类驻留在App_Code之外的其他地方,请向您的解决方案中添加一个“类库”项目,并将该类放在那里。 Don't forget to reference that project in your webproject. 不要忘记在您的Web项目中引用该项目。

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

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 事先进行一些准备(构建/发布),并在第一次现场访问时进行实际编译

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). 将代码文件(与aspx文件相关的文件除外)放在App_Code文件夹中只是为了确保它们的编译(这是ASP.net强制执行的要求,现在我从注释中知道它仅适用于web site项目,而不适用于web application项目)。 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. 检查,您将找不到App_Code文件夹外部的代码文件的“ Build Action

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM