ASP.NET 无法引用类
我已将 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果您希望您的类位于 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.
仅编译 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
代码文件(与 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 forweb site
projects and notweb application
projects). If they are anywhere else they are not compiled. Check and you will not findBuild Action
for the code files outsideApp_Code
folder.