2009-10-20 120 views
3

我在对System.Web标签之间我的web.config文件中的以下两个配置文件属性:无法从web.config访问自定义配置文件属性?

<anonymousIdentification enabled="true"/> 
<profile> 
    <properties> 
    <add name="CustomerName" allowAnonymous="true"/> 
    <add name="CustomerID" allowAnonymous="true"/> 
    </properties> 
</profile> 

当我尝试从aspx.cs文件访问Profile.CustomerName或Profile.CustomerID,它没有出现。我想当你创建一个自定义配置文件属性时,它会自动更新aspnetdb的新属性。配置文件也不会在智能感知中显示。

+0

检查下面 – 2012-08-08 08:46:01

回答

2

如果您正在创建Web应用程序,则会出现此问题。与网站不同,Web Apps不会生成ProfileCommon类。 您既可以按照这个方法: http://webproject.scottgu.com/CSharp/Migration2/Migration2.aspx (附录2)

我没有亲自使用上述组件,所以别人可能会给对一些建议。 但是,你也可以尝试:

Object customerName = HttpContext.Current.Profile.GetPropertyValue("CustomerName") 

你不会用这种方法得到的智能感知,但只要你知道属性的名称,它可以像访问。

+0

我的回答不是继承ProfileBase和创建自己的类的,我想你已经提供的方法是最简单的方法。您还可以使用 - HttpContext.Current.Profile.SetPropertyValue(“CustomerName”,“MyCustomer”)设置属性的值 – 2012-08-08 08:36:48

0

指定在web.config文件属性如下: - ProfilePropertyWeb.config

现在,写了下面的代码: -

Code Behind Profile Properties

编译并运行代码。您将获得以下输出: -

Output

相关问题