如果您使用的是 Web 应用程序而不是网站,则可以使用配置文件提供程序,但从后面的 aspx 页面代码中开箱即用将无法执行此操作Profile.MyProperty
.
这没什么大不了的,因为只要付出一点努力,您就可以做类似的事情。中提供的示例将网站项目转换为 Web 应用程序项目 http://msdn.microsoft.com/en-us/library/aa983476%28v=VS.90%29.aspx是关于如何在 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 应用程序项目。