0
我试图获取用户帐户管理在一个asp.net网站mvc5我建设工作。新用户创建主要工作,但是我在创建用户期间抛出异常时显示错误。该例外被捕获/处理,并且错误信息被添加到我的AdminController的Create()动作的ModelState对象,但是当我返回到创建视图不显示错误。Asp.net MVC5为什么不会Html.ValidationSummary(假)显示异常错误?
其他类型的用户创建错误的,不导致异常被抛出,做正确显示。作为一个侧面说明,当问题发生时,我的浏览器上的“微调器”从不停止,表明发起创建操作的帖子从未完成。如果我点击浏览器中的一个菜单,我会导航到新页面,并且微调控制器消失。
var newUserCreateResult = UserManager.Create(newUser, password)
回报:
newUserCreateResult.Succeeded == false
的创建()的错误,与关联
的时候出现的Create()是正确显示和不与异常相关的错误例外,不正常显示时出现:
await UserManager.SendEmailAsync(newUser.Id, "Reset Password", "Please confirm your account and reset your password by clicking <a href=\"" + callbackUrl + "\">here</a>");
抛出异常。我使用的是SmtpMailService,当无法访问电子邮件服务器或者smtp帐户配置不正确时引发异常。
有些事情我已经试过:
- @ Html.ValidationSummary(真)
- 手动创建通过的ModelState中 错误在创建视图迭代的错误列表。调试器指示代码执行, 但它仍然不显示。
我的Create()行为:
// PUT: /Admin/Create
[Authorize(Roles = "Admin")]
[HttpPost]
[ValidateAntiForgeryToken]
#region public ActionResult Create(CreateUserDTO createUserDTO)
public async Task<ActionResult> Create(CreateUserDTO createUserDTO) {
ApplicationUser newUser = null;
try {
if (createUserDTO == null) {
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
var Email = createUserDTO.Email.Trim();
if (Email == "") {
throw new Exception("No Email");
}
// Create user
newUser = new ApplicationUser { UserName = Email, Email = Email };
var password = Membership.GeneratePassword(8, 3) + "aaZZ09";
var newUserCreateResult = UserManager.Create(newUser, password);
if (newUserCreateResult.Succeeded == true) {
UserManager.AddToRole(newUser.Id, createUserDTO.SelectedRole);
string code = await UserManager.GeneratePasswordResetTokenAsync(newUser.Id);
code = HttpUtility.UrlEncode(code);
var callbackUrl = Url.Action("ConfirmEmail", "Account", new { userId = newUser.Id, code = code }, protocol: Request.Url.Scheme);
await UserManager.SendEmailAsync(newUser.Id, "Reset Password", "Please confirm your account and reset your password by clicking <a href=\"" + callbackUrl + "\">here</a>");
return Redirect("~/Admin");
} else {
// Html.ValidationSummary displays these errors correctly
foreach (string msg in newUserCreateResult.Errors) {
ModelState.AddModelError(string.Empty, msg);
}
}
} catch (Exception ex) {
// Html.ValidationSummary DOESN NOT display these errors
ModelState.AddModelError(string.Empty, ex.Message);
}
if (newUser != null) {
UserManager.Delete(newUser);
}
createUserDTO.Roles = GetAllRolesAsSelectList();
return View("Create", createUserDTO);
}
我Create.cshtml观点:
@model Nissan.Models.CreateUserDTO
@{
Layout = "~/Views/Shared/_Layout.cshtml";
}
@using (Html.BeginForm("Create", "Admin", FormMethod.Post, new { @class = "form-horizontal", role = "form" })) {
@Html.AntiForgeryToken()
<div class="form-horizontal">
<h4>Create User</h4>
<hr />
@Html.ValidationSummary(false, "", new { @class = "text-danger" })
<div class="form-group">
@Html.LabelFor(model => model.Email,
htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Email,
new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Email, "",
new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Roles,
htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.DropDownListFor(model => model.SelectedRole, (Model == null) ? null : Model.Roles);
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Create" class="btn btn-default" />
@Html.ActionLink("Cancel",
"Index",
null,
new { @class = "btn btn-default" })
</div>
</div>
</div>
}