Web Site 支持设计时使用动态根据webconfig配置的property生成的 ProfileCommon,但是 Web App 不具备此特性,因为两者的编译模式不一样。

假如在 web.config 做如下配置:

[code=xml;自定义Profile]

    <system.web>

      <profile>

        <properties>

          <add name="FirstName"/>

          <add name="LastName"/>

        </properties>       

      </profile>

[/code]

在 Web Site 你可以使用:

[code=c# WebSiteSample]

public partial class _Default : System.Web.UI.Page

{

    protected void Page_Load(object sender, EventArgs e)

    {

        Profile.FirstName = "Leo";   // ProfileCommon generated dynamicallyss    

        Profile.LastName = "Wang";          

    }

}

[/code]

在 WebApp 只有使用

[code=C#;Web App Sample]

public partial class _Default : System.Web.UI.Page

{

    protected void Page_Load(object sender, EventArgs e)

    {

        Context.Profile.SetPropertyValue("FirstName", "Leo");

        Context.Profile.SetPropertyValue("LastName", "Wang");


    }

}


[/code]

既然 Web Site 的 ProfileCommon 是动态生成,那我们当然可以自己手动生成一个 ProfileCommon 来支持强类型,具体见

但是,假如属性很多,手动生成工作量大维护也困然,幸运的是已经有插件来支持自动化生成:Scott 在 ​​Upgrading VS 2005 Web Site Projects to be VS 2005 Web Application Project​​ 提到了 ​​ASP.NET WebProfile Generator ​

下载之后执行安装。打开VS,新建一个WebApp项目,在 Solution Explorer 中右击 web.config,会出现 Generate Web Profile,点击之后会在项目根目录生成一个WebProfile.cs 。具体用法见其中的 readme.txt 。

注意这个插件是针对 VS2005 的,如果是 VS 2008 需要稍微修改一下:

1. 打开 WebProfileGenerator 项目

2. 将 WebProfileGenerator.AddIn 文件中 version 修改为 9.0

3. Build

4. 将 WebProfileGenerator.AddIn 以及生成的 WebProfileGenerator.dll 拷入 D:\Users\Administrator\Documents\Visual Studio 2008 文件夹

备注:我的是在:C:\Documents and Settings\Administrator\My Documents\Visual Studio 2008下新建了个AddIns文件夹然后将WebProfileGenerator.dll

和WebProfileGenerator.AddIn 文件copy进去.

5. 重新打开 VS,进入菜单/Tools/Add-In Manager,应该可以看见 ASP.NET Web Profile Generator 一项,如果没有启用,则启用它。

另外,​​菩提树下的杨过​​​ 介绍了另一个插件:​http://code.msdn.microsoft.com/WebProfileBuilder">​http://code.msdn.microsoft.com/WebProfileBuilder​

在此做了几个简单的例子,仅共参考: ​​下载示例​​​ ​​ASP.NET WebProfile Generator 下载​

作者:​​新瓶老酒​

申明:本文版权归作者所有,欢迎,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。