我是asp.net的新手.我正在尝试使用WebMatrix创建一个简单的登录和注册网页.但是当我尝试创建帐户时出现以下错误:
The Provider encountered an unknown error. Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. Exception Details: System.Web.Security.MembershipCreateUserException: The Provider encountered an unknown error. Source Error: Line 32: db.Execute("INSERT INTO UserData (Email,Name) VALUES (@0,@1)",email,username); Line 33: Line 34: WebSecurity.CreateAccount(email,password); Line 35: Response.Redirect("Homepage.cshtml"); Line 36: } Source File: c:\Users\admin\Documents\My Web Sites\Login\Register.cshtml Line: 34 Stack Trace: [MembershipCreateUserException: The Provider encountered an unknown error.] WebMatrix.WebData.SimpleMembershipProvider.CreateAccount(String userName,String password,Boolean requireConfirmationToken) +1312 WebMatrix.WebData.WebSecurity.CreateAccount(String userName,Boolean requireConfirmationToken) +31 ASP._Page_Register_cshtml.Execute() in c:\Users\admin\Documents\My Web Sites\Login\Register.cshtml:34 System.Web.WebPages.WebPageBase.ExecutePageHierarchy() +207 System.Web.WebPages.WebPage.ExecutePageHierarchy(IEnumerable`1 executors) +68 System.Web.WebPages.WebPage.ExecutePageHierarchy() +156 System.Web.WebPages.StartPage.RunPage() +19 System.Web.WebPages.StartPage.ExecutePageHierarchy() +65 System.Web.WebPages.WebPageBase.ExecutePageHierarchy(WebPageContext pageContext,TextWriter writer,WebPageRenderingBase startPage) +76 System.Web.WebPages.WebPageHttpHandler.ProcessRequestInternal(HttpContextBase httpContext) +119 Version Information: Microsoft .NET Framework Version:4.0.30319; ASP.NET Version:4.0.30319.272
任何帮助表示赞赏.谢谢.
解决方法
务必:
public ActionResult SignUp(SignUpModel model) { if (ModelState.IsValid) { using (MorphometryContext context = new MorphometryContext()) { User user = new User { Email = model.Email,FirstName = model.FirstName,LastName = model.LastName,Username = model.UserName }; context.Users.Add(user); context.SaveChanges(); } // Attempt to register the user try { WebSecurity.CreateAccount(model.UserName,model.Password); } catch (MembershipCreateUserException e) { ModelState.AddModelError("",ErrorCodeToString(e.StatusCode)); return View(); } WebSecurity.Login(model.UserName,model.Password); return RedirectToAction("Index","Projects"); }
此外,您不希望Respoonse.Redirect到.cshtml文件.相反,您应该返回RedirectToAction并传入Action和Controller名称.