2013-12-12 144 views
0

these说明我能为一个配置文件添加此到Web.config中定义自定义属性:自定义配置文件属性

<profile defaultProvider="DefaultProfileProvider"> 
    <providers> 
     <add 
     name="DefaultProfileProvider" 
     type="System.Web.Providers.DefaultProfileProvider, System.Web.Providers, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" 
     connectionStringName="MYCONEXION" 
     applicationName="MyApp" /> 
    </providers> 
    <properties> 
    <add name="IdRole" type="Integer" /> 
    <add name="IdUser" type="Integer" /> 
    </properties> 
</profile> 

我发现的问题是,我可以访问此属性(Profile.IdRole)时分组码采用嵌入式在脚本中,在块<script language="vb" runat="server">

当代码块在CodeBehind中时,它看起来像Profile实际上是一个不同的对象/类。另外,如果我将代码从CodeBehind移动到嵌入块,我遇到了一些其他类和方法的问题,例如Security.Cryptography.SHA1CryptoServiceProvider或我自己项目中的模块中的函数。

我想我错过了一些前缀到Profile.IdRole从CodeBehind访问它时,你能帮忙吗?

错误信息:

在代码隐藏,当我使用Profile.IdRole我得到错误IdRole不是档案的成员。

如果我使用HttpContext.Current.Profile.IdRole我得到IdRole不System.Web.Profile.ProfileBase

当我使用嵌入式代码Profile.IdRole,将鼠标在它的成员,我得到一个tooltop:只读保护Property Profile作为ProfileCommon,现在我看到ReadOnly我不确定我是否遵循正确的文档...帮助?

谢谢

+0

尝试'HttpContext.Current.Profile'? – RobH

+0

不,不同的错误,但也是一个错误,我会更新这个信息的问题 –

+0

你是否在你的代码中使用VB或C#? – RobH

回答

0

的ProfileCommon类是强类型类,从ProfileBase继承。它在运行时创建,并使用在您的Web配置文件中定义的定制属性信息。由于它是在运行时创建的,因此它不能直接从代码隐藏中访问。

从后台代码,访问当前用户的配置文件,使用

HttpContext.Current.Profile 

要访问其他用户的个人资料,使用

System.Web.Profile.ProfileBase.Create(username) 

两个函数返回ProfileBase类的一个实例。由于它不使用你的自定义属性强类型,你必须使用GetPropertyValueSetPropertyValue方法,像这样

Dim profile as ProfileBase = ProfileBase.Create("someuser") 
profile.GetPropertyValue("IdRole") 
profile.SetPropertyValue("IdRole", 42)