【问题标题】:ASP .NET MVC Custom Profile Settings Not Found Issue找不到 ASP .NET MVC 自定义配置文件设置问题
【发布时间】:2011-07-08 19:12:01
【问题描述】:

目前我在我的 ASP .NET MVC 应用程序中使用自定义配置文件。创建用户和设置配置文件属性时一切正常。我遇到的问题是,当我编辑用户时,我不断收到 settings property 'FirstName' was not found 错误。

这是我正在使用的代码,据我了解,它应该可以正常工作:

用户配置文件类

public class UserProfile : ProfileBase
{
    [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; }
    }

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

    public static UserProfile GetUserProfile(string username)
    {
        return Create(username) as UserProfile;
    }
}

Web.config

<membership>
  <providers>
    <clear/>
    <add name="AspNetSqlMembershipProvider" type="System.Web.Security.SqlMembershipProvider" connectionStringName="ApplicationServices"
         enablePasswordRetrieval="false" enablePasswordReset="true" requiresQuestionAndAnswer="false" requiresUniqueEmail="false"
         maxInvalidPasswordAttempts="5" minRequiredPasswordLength="6" minRequiredNonalphanumericCharacters="0" passwordAttemptWindow="10"
         applicationName="/" />
  </providers>
</membership>

<profile inherits="MissionManager.Models.UserProfile">
  <providers>
    <clear/>
    <add name="AspNetSqlProfileProvider" type="System.Web.Profile.SqlProfileProvider" connectionStringName="ApplicationServices" applicationName="/" />
  </providers>
</profile>

调用代码 - 请注意,在我使用此方法之前,ASP .NET 会遇到上述错误。

   [HttpPost]
    public ActionResult Edit(UserModel model)
    {
        try
        {
            MembershipService.UpdateUser(User.Identity.Name, model);
            return RedirectToAction("Index");
        }
        catch
        {
            return View();
        }
    }

【问题讨论】:

    标签: asp.net-mvc-3


    【解决方案1】:

    我有类似的东西。区别在于:

    public string FirstName
    {
        get { return this["FirstName"] as string; }
        set { this["FirstName"] = value; }
    }
    
    public string LastName
    {
        get { return this["LastName"] as string; }
        set { this["LastName"] = value; }
    }
    

    我没有自定义属性的属性,我使用 this 插入 base

    HTH

    【讨论】:

    • 刚刚尝试过,仍然遇到同样的问题。创建用户后,我还检查了数据库,并且配置文件很好地保存了属性名称。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-05-15
    • 2013-07-20
    • 1970-01-01
    • 2011-11-29
    • 1970-01-01
    • 2010-11-18
    • 2010-10-29
    相关资源
    最近更新 更多