这个问题在这里已经有了答案:
10年前关闭。
Possible Duplicate:
How to assign Profile values?
我正在读一本 ASP.NET 的书,上面说如果在启动项目时选择 Web 应用程序,就不能使用 Profile。它只在网站下运行。
Web 应用程序有任何替代方案吗?或者您是否需要构建自己的 Profile 系统。
最佳答案
如果您使用的是 Web 应用程序而不是网站,则可以使用配置文件提供程序,但是从背后的 aspx 页面代码开箱即用将无法执行 Profile.MyProperty
.
这没什么大不了的,只要稍加努力,您就可以做类似的事情。 Converting a Web Site Project to a Web Application Project中提供的示例是关于如何在 Web 应用程序中使用配置文件提供程序的一个很好的起点。
总结上述文章的转换配置文件对象代码部分,创建一个 ProfileCommon
类(class)
public class ProfileCommon
{
public Teachers Teachers
{
get
{
return (Teachers)
HttpContext.Current.Profile.GetPropertyValue("Teachers");
}
set
{
HttpContext.Current.Profile.SetPropertyValue("Teachers",value);
}
}
}
然后在你后面的aspx代码上你现在可以做
ProfileCommon Profile = new ProfileCommon();
protected void Button1_Click(object sender, EventArgs e)
{
Teachers teachers = new Teachers();
teachers.Add(new Teacher("scott"));
teachers.Add(new Teacher("bob"));
teachers.Add(new Teacher("paul"));
Profile.Teachers = teachers;
}
实例化
ProfileCommon
的替代方法作为每个页面的字段将使其及其方法静态,只需从背后的代码中调用类属性 Profile.Teachers
public static class Profile
{
public static Teachers Teachers
{
//......
}
}
这不是一个巨大的优势,但会使您的代码更类似于 ASP.NET 网站项目的代码。
与答案无关的社论评论
两种项目类型之间存在关键差异,但对于我的项目,我更喜欢 Web 应用程序。我有一个偏见,因为为 Web 应用程序项目创建 TFS 构建管理器的构建配置文件比为网站项目创建要容易得多。此外,微软似乎强调了网站项目,然后又放弃了它们,转而支持 Web 应用程序项目。
关于asp.net - 在 ASP.NET Web 应用程序中使用配置文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5450339/