Asp.net mvc 4 配置文件提供程序不在MVC4 ASP.net中工作
我的web.conf文件如下:Asp.net mvc 4 配置文件提供程序不在MVC4 ASP.net中工作,asp.net-mvc-4,asp.net-profiles,Asp.net Mvc 4,Asp.net Profiles,我的web.conf文件如下: 有助于添加更多解释。帮助我澄清了剩下的问题 ASP.NET MVC 4使用新的SimpleMembershipProvider。下面介绍如何将自定义字段添加到自动生成的模板和用户配置文件中 使用Internet模板创建新的ASp.NET MVC 4应用程序 导航到~/Models/AccountModel.cs文件,并将字段添加到UserProfile类: [Table("UserProfile")] public class UserProfile {
有助于添加更多解释。帮助我澄清了剩下的问题 ASP.NET MVC 4使用新的SimpleMembershipProvider
。下面介绍如何将自定义字段添加到自动生成的模板和用户配置文件中
使用Internet模板创建新的ASp.NET MVC 4应用程序
导航到~/Models/AccountModel.cs
文件,并将字段添加到UserProfile
类:
[Table("UserProfile")]
public class UserProfile
{
[Key]
[DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
public int UserId { get; set; }
public string UserName { get; set; }
// New fields added to store profile information
public string FirstName { get; set; }
public string LastName { get; set; }
public string Site { get; set; }
}
将新字段添加到RegisterModel
视图模型:
public class RegisterModel
{
[Required]
[Display(Name = "User name")]
public string UserName { get; set; }
[Required]
[StringLength(100, ErrorMessage = "The {0} must be at least {2} characters long.", MinimumLength = 6)]
[DataType(DataType.Password)]
[Display(Name = "Password")]
public string Password { get; set; }
[DataType(DataType.Password)]
[Display(Name = "Confirm password")]
[Compare("Password", ErrorMessage = "The password and confirmation password do not match.")]
public string ConfirmPassword { get; set; }
// New fields added to store profile information
public string FirstName { get; set; }
public string LastName { get; set; }
public string Site { get; set; }
}
修改~/Views/Account/Register.cshtml
视图,允许用户在注册时输入这些附加字段:
@model MvcApplication1.Models.RegisterModel
@{
ViewBag.Title = "Register";
}
<hgroup class="title">
<h1>@ViewBag.Title.</h1>
<h2>Create a new account.</h2>
</hgroup>
@using (Html.BeginForm()) {
@Html.AntiForgeryToken()
@Html.ValidationSummary()
<fieldset>
<legend>Registration Form</legend>
<ol>
<li>
@Html.LabelFor(m => m.UserName)
@Html.TextBoxFor(m => m.UserName)
</li>
<li>
@Html.LabelFor(m => m.FirstName)
@Html.TextBoxFor(m => m.FirstName)
</li>
<li>
@Html.LabelFor(m => m.LastName)
@Html.TextBoxFor(m => m.LastName)
</li>
<li>
@Html.LabelFor(m => m.Site)
@Html.TextBoxFor(m => m.Site)
</li>
<li>
@Html.LabelFor(m => m.Password)
@Html.PasswordFor(m => m.Password)
</li>
<li>
@Html.LabelFor(m => m.ConfirmPassword)
@Html.PasswordFor(m => m.ConfirmPassword)
</li>
</ol>
<input type="submit" value="Register" />
</fieldset>
}
@section Scripts {
@Scripts.Render("~/bundles/jqueryval")
}
按Ctrl+F5启动应用程序并创建新用户
数据库中的UserProfile
表将填充其他字段
最后,当以后您需要访问某个用户(例如当前经过身份验证的用户)的那些属性时:
ASP.NET MVC 4使用新的SimpleMembershipProvider
。下面介绍如何将自定义字段添加到自动生成的模板和用户配置文件中
使用Internet模板创建新的ASp.NET MVC 4应用程序
导航到~/Models/AccountModel.cs
文件,并将字段添加到UserProfile
类:
[Table("UserProfile")]
public class UserProfile
{
[Key]
[DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
public int UserId { get; set; }
public string UserName { get; set; }
// New fields added to store profile information
public string FirstName { get; set; }
public string LastName { get; set; }
public string Site { get; set; }
}
将新字段添加到RegisterModel
视图模型:
public class RegisterModel
{
[Required]
[Display(Name = "User name")]
public string UserName { get; set; }
[Required]
[StringLength(100, ErrorMessage = "The {0} must be at least {2} characters long.", MinimumLength = 6)]
[DataType(DataType.Password)]
[Display(Name = "Password")]
public string Password { get; set; }
[DataType(DataType.Password)]
[Display(Name = "Confirm password")]
[Compare("Password", ErrorMessage = "The password and confirmation password do not match.")]
public string ConfirmPassword { get; set; }
// New fields added to store profile information
public string FirstName { get; set; }
public string LastName { get; set; }
public string Site { get; set; }
}
修改~/Views/Account/Register.cshtml
视图,允许用户在注册时输入这些附加字段:
@model MvcApplication1.Models.RegisterModel
@{
ViewBag.Title = "Register";
}
<hgroup class="title">
<h1>@ViewBag.Title.</h1>
<h2>Create a new account.</h2>
</hgroup>
@using (Html.BeginForm()) {
@Html.AntiForgeryToken()
@Html.ValidationSummary()
<fieldset>
<legend>Registration Form</legend>
<ol>
<li>
@Html.LabelFor(m => m.UserName)
@Html.TextBoxFor(m => m.UserName)
</li>
<li>
@Html.LabelFor(m => m.FirstName)
@Html.TextBoxFor(m => m.FirstName)
</li>
<li>
@Html.LabelFor(m => m.LastName)
@Html.TextBoxFor(m => m.LastName)
</li>
<li>
@Html.LabelFor(m => m.Site)
@Html.TextBoxFor(m => m.Site)
</li>
<li>
@Html.LabelFor(m => m.Password)
@Html.PasswordFor(m => m.Password)
</li>
<li>
@Html.LabelFor(m => m.ConfirmPassword)
@Html.PasswordFor(m => m.ConfirmPassword)
</li>
</ol>
<input type="submit" value="Register" />
</fieldset>
}
@section Scripts {
@Scripts.Render("~/bundles/jqueryval")
}
按Ctrl+F5启动应用程序并创建新用户
数据库中的UserProfile
表将填充其他字段
最后,当以后您需要访问某个用户(例如当前经过身份验证的用户)的那些属性时:
如果您正在使用新的Microsoft.AspNet.Providers包,那么提供程序类型不应该是System.Web.Providers.DefaultProfileProvider
而不是System.Web.Profile.SqlProfileProvider
?您使用哪一组成员资格api提供程序作为成员资格和角色?@danludwig I没有为成员资格或角色指定提供程序,它们使用现有数据库。使用此提供程序时,我更新了上面的问题,出现了新错误。如果您使用的是新的Microsoft.AspNet.Providers包,那么提供程序类型不应该是System.Web.Providers.DefaultProfileProvider
而不是System.Web.Profile.SqlProfileProvider
?您使用哪一组成员资格api提供程序作为成员资格和角色?@danludwig I没有为成员资格或角色指定提供程序,它们使用现有数据库。使用此提供程序时,我更新了上面的问题,出现了新错误。感谢提供详细信息。我是ASP.Net新手,所以这对我帮助很大。我假设角色仍然单独处理并在web.config中设置。至少这是我设置它们的方式,而且它们似乎正在工作。感谢您提供的详细信息。我是ASP.Net新手,所以这对我帮助很大。我假设角色仍然单独处理并在web.config中设置。至少这是我设置它们的方式,它们似乎正在工作。