目前我在ASP .NET MVC应用程序中使用自定义配置文件。创建用户和设置配置文件属性时,一切正常。我遇到的问题是,当我编辑用户时,我一直在找到设置属性'找不到FirstName'错误。
这是我正在使用的代码,根据我的理解,它应该可以正常工作:
用户个人资料类
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();
}
}
答案 0 :(得分: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
的{{1}}实例读取
HTH