2013-02-28 82 views
0

我正在使用MVC4,我想在配置文件中存储一些值。配置文件提供程序和属性已设置,但我无法访问配置文件属性

为什么

TempData["badgerName"] = Profile.BadgerName; 

说ProfileBase不包含BadgerName的定义?

我已经设置了Profile如下。

<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="FALContext" 
     applicationName="/" /> 
    </providers> 
    <properties> 
    <add name="BadgerName" type="String"/> 
    </properties> 
</profile> 

回答

1

在ASP MVC你不必为您的网站在web.config文件中定义的属性生成的配置文件对象。可以通过控制器方法访问的属性Profile的类型为ProfileBasesee msdn),并且不包含您的自定义配置文件属性的强类型属性。您也可能知道,在请求开始时为登录用户加载此配置文件,并且在请求结束时保存所有更改。

different ways你可以工作ProfileBase类。最常用的的是:

  • 直接使用的ProfileBase类
  • 创建一个派生自定义配置文件类。

当直接使用ProfileBase时,您需要使用控制器方法中的实例或获取给定用户名的实例。然后,您应该使用索引器访问配置文件属性。比方说,你的控制器的方法收到类型的usermodel的对象,其中包含如电子邮件和BadgerName您的用户数据,那么你可以这样写代码:

//Getting the instance from the controller property: 
ProfileBase profile = this.Profile; //or even: this.HttpContext.Profile 
//You can also get the profile for a given existing user. 
ProfileBase profile = ProfileBase.Create(userModel.Name); 

//Then update properties using indexer 
profile["Email"] = userModel.Email; 
profile["BadgerName"] = userModel.BadgerName; 
//Manually save changes 
//(Can be skipped for the profile automatically loaded in the Controller) 
profile.Save(); 

但是如果从ProfileBase创建一个派生类,你会最终以与你原先想要的相同的方式使用你的班级。您将基本建立与内部访问使用ProfileBase索引强类型属性的包装类(方法的总结here):

public class MyCustomProfile : ProfileBase 
{      
    public string Email 
    {  
     get { return base["Email"] as string; }  
     set { base["Email"] = value; } 
    } 

    public string BadgerName 
    {  
     get { return base["BadgerName"] as string; }  
     set { base["BadgerName"] = value; } 
    } 

    //If needed, you can provide methods to recover profiles 
    //for the logged in user or any user given its user name 
    public static MyCustomProfile GetCurrent() 
    { 
     return Create(Membership.GetUser().UserName) as MyCustomProfile; 
    } 

    public static MyCustomProfile GetProfile(string userName)  
    { 
     return Create(userName) as MyCustomProfile;  
    } 
} 

如果使用这个选项,你还需要确保<profile>元素在web.config中有inherits属性设置为自定义模型类:

<profile enabled="true" defaultProvider="DefaultProfileProvider" inherits="yourNamespace.MyCustomProfile"> 

有了这个代码和配置到位,您可以开始使用您的自定义配置文件类由要么恢复用户配置文件yourselve或铸造控制器配置文件属性到您的自定义类:

//Cast the Profile property of the controller to your custom class 
MyCustomProfile profile = this.Profile as MyCustomProfile // or even: HttpContext.Profile as MyCustomProfile 
//You could also manually load the profile for given an user name 
profile = MyCustomProfile.GetProfile(userModel.Name); 
//Or even manually load the profile for the logged in user 
profile = MyCustomProfile.GetCurrent(); 

//Now get/set the profile properties using the strongly typed properties of your class 
profile.Email= userModel.Email; 
profile.BadgerName= userModel.BadgerName; 
//Manually save changes 
//(Can be skipped for the profile automatically loaded in the Controller) 
profile.Save(); 

希望这有助于!

+0

很好的答案,谢谢。 – orangesherbert 2013-02-28 23:24:05