简体   繁体   中英

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.

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?)

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.

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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