2011-11-28 88 views
3

我在这里碰到了一个泡菜。我有一个自定义配置文件,我在两个应用程序中使用了相同的解决方案。第一个是构建的Web应用程序,用于将旧的.net应用程序中的一组自定义用户配置文件和属性导入到.net成员身份,角色,配置文件表中。我构建了一个从配置文件继承的配置文件公共类。这两个应用程序在其命名空间中都有相同类的副本。.net配置文件提供程序和自定义配置文件。设置属性“'未找到。

using System; 
using System.Web.Security; 
using System.Web.Profile; 
using System.Collections.Specialized; 


namespace WebProject 
{ 
    public class ProfileCommon : ProfileBase 
    { 
     public static ProfileCommon GetUserProfile(string username) 
     { 
      return Create(username) as ProfileCommon; 
     } 

     public static ProfileCommon GetUserProfile() 
     { 
      return Create(Membership.GetUser().UserName) as ProfileCommon; 
     } 

     [SettingsAllowAnonymous(false)] 
     public string FirstName 
     { 
      get 
      { 
       return base["FirstName"] as string; 
      } 
      set 
      { 
       base["FirstName"] = value; 
      } 
     } 

     [SettingsAllowAnonymous(false)] 
     public string LastName 
     { 
      get 
      { 
       return base["LastName"] as string; 
      } 
      set 
      { 
       base["LastName"] = value; 
      } 
     } 

     [SettingsAllowAnonymous(false)] 
     public string Email 
     { 
      get 
      { 
       return base["Email"] as string; 
      } 
      set 
      { 
       base["Email"] = value; 
      } 
     } 

     [SettingsAllowAnonymous(false)] 
     public StringCollection Sites 
     { 
      get 
      { 
       return base["Sites"] as StringCollection; 
      } 

      set 
      { 
       base["Sites"] = value; 
      } 
     } 
    } 
} 

我在我的web配置文件中的配置文件提供程序部分看起来像这样。

<profile defaultProvider="WebProjectProfileProvider" inherits="WebProject.ProfileCommon"> 
    <providers> 
    <clear /> 
    <add name="WebProjectProfileProvider" applicationName="/" type="System.Web.Profile.SqlProfileProvider" connectionStringName="Test"/> 
    </providers> 
</profile> 

如果我使用一个应用程序来执行用户导入,另一个使用的会员,角色和我创建的配置文件会变成这样导致“设置属性‘’没有被发现。”错误?我似乎无法查明错误发生的位置以及我已检查的一些最常见原因。这是我第一次在如此大规模的.net中使用这个功能。任何帮助是极大的赞赏。

谢谢。

+0

你在哪里指定了可用的设置名称和类型?如果我没有记错,它们也应该在Web.config文件中指定。 – Venemo

+0

指定的名称和类型在Profile Common类本身内。因为我在web.config中使用。它会自动知道使用上面列出的Profile Common类中的类型。如果我不使用“inherits”属性,则.net框架将在运行时抛出一个错误,指出该属性未被声明。如果不使用inherits属性,则开发人员必须使用节点在web.config文件中声明名称和类型。 – gsirianni

回答

1

我发现我的问题。问题出在调用代码中。我曾遇到关于我忘了改调用代码回到静态方法

ProfileCommon.GetUserProfile(); 

其他问题我曾经碰到以及在web配置声明文件的属性,并宣布轮廓这么多问题他们在一个配置文件的公共类。这导致我得到翻转错误,例如“该财产已被定义”。和“设置属性”未找到。“

简而言之,如果您使用的是“Web应用程序”解决方案,请在代码中声明ProfileCommon代理类,而不是在web.config中声明。如果您使用的是“网站”解决方案,请在web.config中声明属性。

我在网上发现的最好的例子就是来自这个网站。

ASP.NET Profiles in Web Application Projects

它描述了如何在一个不错的简明摘要使用自定义配置文件,并给出了为什么Web应用程序执行该方法以及它为什么做不同的网站的完整说明。希望这可以节省许多头痛。

+0

http://weblogs.asp.net/jongalloway/writing-a-custom-asp-net-profile-class – KRob

相关问题