VS 2005 Web Application Project 项目中Profile的使用方法(更新)
在VS2005 Web Site Projects项目中, 如果在Web.config文件中定义了Profile节,那么ASP.Net会自动为项目中的每个页面增加一个Profile的对象类型,这个对象类型完成了对定义在Web.config文件Profile节定义的所有属性进行强类型映射。开发员能够通过智能感知获得访问者的Profile信息,例如:
在编写程序的时候,开发人员可以这样写:
这种支持是使用VS2005 Web Site Project选项,动态建立和增加一个ProfileCommon类对象Profile到每一个Code-behind实例。
现在VS2005 Web Application Project 1.0英文版是不支持动态建立Profile对象,如果使用的项目是Web Applicaiton Project则不能发现Profile对象
解决的方法有两种
********************************************************************************
一、自己Coding ProfileCommon类
public class ProfileCommon : System.Web.Profile.ProfileBase
{
[SettingsAllowAnonymous(true)]
public string TestString
{
get { return base["TestString"] as string; }
set { base["TestString"] = value; }
}
}
在Web.config文件中要这样定义
<profile defaultProvider="SqlServers" enabled="true" inherits="ProfileCommon" /> inherits属性的ProfileCommon如果有命名空间必须加入如:web.ProfileCommon
Code-Behind中
ProfileCommon profile = (ProfileCommon)HttpContext.Current.Profile;
profile.TestString ="Test";
********************************************************************************
********************************************************************************
二、使用第三方的Add-in : ASP.Net WebProfile Generator
什么是Asp.Net WebProfile Generator?
The Web Profile Generator is an add-in for Visual Studio 2005 that generates a strongly typed class for accessing the ASP.NET profile in Web Application
Projects. Because Web Application Projects compile the code behind files in advance of ASP.NET they do not have access to dynamically generated types like the Profile object.
其实这个就是解决不能支持动态建立Profile对象的Add-in。
在使用时:
1.安装 Asp.net WebProfile Generator Add-in.
2.在工程中建议web.config并进行配置<profile><properties>等
3.右击web.config文件有一个option:Generator WebProfile ,生成WebProfile.cs
4. Profile.MyProperty就可以调用在web.Config中配置中配置项。 * MyProperty指配置项目的Name
*如果了解Profile的其他特性以下有两个的Link:
http://quickstarts.asp.net/QuickStartv20/aspnet/doc/profile/default.aspx
http://msdn2.microsoft.com/en-us/library/ms379605(VS.80).aspx
http://www.gotdotnet.com/Workspaces/Workspace.aspx?id=406eefba-2dd9-4d80-a48c-b4f135df4127
解决的方法有两种
********************************************************************************
一、自己Coding ProfileCommon类
public class ProfileCommon : System.Web.Profile.ProfileBase
{
[SettingsAllowAnonymous(true)]
public string TestString
{
get { return base["TestString"] as string; }
set { base["TestString"] = value; }
}
}
在Web.config文件中要这样定义
<profile defaultProvider="SqlServers" enabled="true" inherits="ProfileCommon" /> inherits属性的ProfileCommon如果有命名空间必须加入如:web.ProfileCommon
Code-Behind中
ProfileCommon profile = (ProfileCommon)HttpContext.Current.Profile;
profile.TestString ="Test";
********************************************************************************
********************************************************************************
二、使用第三方的Add-in : ASP.Net WebProfile Generator
什么是Asp.Net WebProfile Generator?
The Web Profile Generator is an add-in for Visual Studio 2005 that generates a strongly typed class for accessing the ASP.NET profile in Web Application
Projects. Because Web Application Projects compile the code behind files in advance of ASP.NET they do not have access to dynamically generated types like the Profile object.
其实这个就是解决不能支持动态建立Profile对象的Add-in。
在使用时:
1.安装 Asp.net WebProfile Generator Add-in.
2.在工程中建议web.config并进行配置<profile><properties>等
3.右击web.config文件有一个option:Generator WebProfile ,生成WebProfile.cs
4. Profile.MyProperty就可以调用在web.Config中配置中配置项。 * MyProperty指配置项目的Name
*如果了解Profile的其他特性以下有两个的Link:
http://quickstarts.asp.net/QuickStartv20/aspnet/doc/profile/default.aspx
http://msdn2.microsoft.com/en-us/library/ms379605(VS.80).aspx
http://www.gotdotnet.com/Workspaces/Workspace.aspx?id=406eefba-2dd9-4d80-a48c-b4f135df4127
ASP.Net WebProfile Generator
Tim McBride has published a cool sample that will generate a WebProfile class for accessing the ASP.NET profile object from within the code behind files of a Web Application Project.
http://www.gotdotnet.com/Workspaces/Workspace.aspx?id=406eefba-2dd9-4d80-a48c-b4f135df4127
Because Web Application Projects compile the code behind files in advance of ASP.NET they do not have access to dynamically generated types like the Profile object. This sample provided an add-in into VS that will generate a WebProfile proxy class that will fetch profile information from the real ASP.NET profile object at runtime.
Hope it helps,
Brad.
Tim McBride has published a cool sample that will generate a WebProfile class for accessing the ASP.NET profile object from within the code behind files of a Web Application Project.
http://www.gotdotnet.com/Workspaces/Workspace.aspx?id=406eefba-2dd9-4d80-a48c-b4f135df4127
Because Web Application Projects compile the code behind files in advance of ASP.NET they do not have access to dynamically generated types like the Profile object. This sample provided an add-in into VS that will generate a WebProfile proxy class that will fetch profile information from the real ASP.NET profile object at runtime.
Hope it helps,
Brad.