C# 在注册页的“throw”语句处获取用户未处理的异常
我正在创建带有密码验证的自定义注册页面。以前我尝试用JavaScript方法来实现这一点,但这些努力都失败了 对于前端验证,我使用Getter和setter调用Re_Enter_Password。现在我尝试在Getter和setter之上添加[NotMapped]属性 问题是,当我尝试使用视图插入注册详细信息时,我在控制器类的throw语句中得到了用户未处理的异常 我使用ADO.NET实体数据模型来访问数据库。我没有在数据库表中定义字段调用重新输入密码 这是我的控制器类:HECController.csC# 在注册页的“throw”语句处获取用户未处理的异常,c#,asp.net,database,C#,Asp.net,Database,我正在创建带有密码验证的自定义注册页面。以前我尝试用JavaScript方法来实现这一点,但这些努力都失败了 对于前端验证,我使用Getter和setter调用Re_Enter_Password。现在我尝试在Getter和setter之上添加[NotMapped]属性 问题是,当我尝试使用视图插入注册详细信息时,我在控制器类的throw语句中得到了用户未处理的异常 我使用ADO.NET实体数据模型来访问数据库。我没有在数据库表中定义字段调用重新输入密码 这是我的控制器类:HECControlle
using AFFEMS2_HEC;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using AFFEMS2_HEC.Models;
using System.Web.Security;
using System.Data.Entity.Validation;
using System.Transactions;
using System.Web.Security;
using DotNetOpenAuth.AspNet;
using Microsoft.Web.WebPages.OAuth;
using WebMatrix.WebData;
using AFFEMS2_HEC.Filters;
using System.Diagnostics;
namespace AFFEMS2_HEC.Controllers
{
public class HECController : Controller
{
public ActionResult Index()
{
return View();
}
[HttpGet]
public ActionResult HEI_Registration()
{
return View();
}
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult HEI_Registration(AFFEMS2_HEC.Models.HEI_REG_TABLE user)
{
try
{
if (ModelState.IsValid)
{
using (var db = new AFFEMS2_HEC.Models.HEI_REG_DBEntities())
{
var crypto = new SimpleCrypto.PBKDF2();
var encrypPass = crypto.Compute(user.Password);
var newUser = db.HEI_REG_TABLE.Create();
newUser.HEI_ID = user.HEI_ID;
newUser.Username = user.Username;
newUser.Title = user.Title;
newUser.Firstname = user.Firstname;
newUser.Lastname = user.Lastname;
newUser.Email = user.Email;
newUser.Direct_Number = user.Direct_Number;
newUser.Mobile_Number = user.Mobile_Number;
newUser.Password = encrypPass;
newUser.PasswordSalt = crypto.Salt;
db.HEI_REG_TABLE.Add(newUser);
db.SaveChanges();
return RedirectToAction("Index", "Home");
}
}
else
{
ModelState.AddModelError("", "Data is not correct");
}
}
catch (DbEntityValidationException e)
{
foreach (var eve in e.EntityValidationErrors)
{
Console.WriteLine("Entity of type \"{0}\" in state \"{1}\" has the following validation errors:",
eve.Entry.Entity.GetType().Name, eve.Entry.State);
foreach (var ve in eve.ValidationErrors)
{
Console.WriteLine("- Property: \"{0}\", Error: \"{1}\"",ve.PropertyName, ve.ErrorMessage);
//Trace.TraceInformation("Property: {0} Error: {1}", ve.PropertyName, ve.ErrorMessage);
}
}
throw ; // Add the original exception as the innerException
}
return View();
}
public ActionResult LogOut()
{
FormsAuthentication.SignOut();
return RedirectToAction("Index", "Home");
}
private bool IsValid(string email, string password)
{
var crypto = new SimpleCrypto.PBKDF2();
bool IsValid = false;
using (var db = new AFFEMS2_HEC.Models.HEI_REG_DBEntities())
{
var user = db.HEI_REG_TABLE.FirstOrDefault(u => u.Email == email);
if (user != null)
{
if (user.Password == crypto.Compute(password, user.PasswordSalt))
{
IsValid = true;
}
}
}
return IsValid;
}
}
}
这是我的模型课:HEI_AccountModels.cs
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
namespace AFFEMS2_HEC.Models
{
public class HEI_REG_DBEntities : DbContext
{
public HEI_REG_DBEntities()
: base("HEI_REG_DBEntities")
{
}
public DbSet<HEI_REG_TABLE> HEI_REG_TABLE { get; set; }
}
[Table("HEI_REG_TABLE")]
public class HEI_REG_TABLE
{
[Key]
[DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
[Required(ErrorMessage = "Please provide ID", AllowEmptyStrings = false)]
public int HEI_ID { get; set; }
[Required(ErrorMessage = "Please provide username", AllowEmptyStrings = false)]
public string Username { get; set; }
[Required(ErrorMessage = "Please provide Title", AllowEmptyStrings = false)]
public string Title { get; set; }
[Required(ErrorMessage = "Please provide first name", AllowEmptyStrings = false)]
public string Firstname { get; set; }
[Required(ErrorMessage = "Please provide last name", AllowEmptyStrings = false)]
public string Lastname { get; set; }
[RegularExpression(@"^([0-9a-zA-Z]([\+\-_\.][0-9a-zA-Z]+)*)+@(([0-9a-zA-Z][-\w]*[0-9a-zA-Z]*\.)+[a-zA-Z0-9]{2,3})$",
ErrorMessage = "Please provide valid email id")]
public string Email { get; set; }
public string Direct_Number { get; set; }
public string Mobile_Number { get; set; }
[Required(ErrorMessage = "Please provide Password", AllowEmptyStrings = false)]
[DataType(System.ComponentModel.DataAnnotations.DataType.Password)]
[StringLength(255, MinimumLength = 5, ErrorMessage = "Password must be 5 char long.")]
public string Password { get; set; }
public string PasswordSalt { get; set; }
[Compare("Password", ErrorMessage = "The password and confirmation password do not match.")]
[DataType(System.ComponentModel.DataAnnotations.DataType.Password)]
[NotMapped]
public string Re_Enter_Password { get; set; }
}
}
这是我的观点:HEI_Registration.cshtml
@model AFFEMS2_HEC.Models.HEI_REG_TABLE
@{
ViewBag.Title = "HEI_Registration";
Layout = "~/Views/Shared/Login_Layout.cshtml";
}
<script src="../../Scripts/jquery-1.7.1.min.js" type="text/javascript"></script>
<script type="text/jscript"></script>
@using (Html.BeginForm())
{
@Html.ValidationSummary(true)
<fieldset>
<legend>User</legend>
@Html.AntiForgeryToken()
@if (ViewBag.Message != null)
{
<div style="border:solid 1px green">
@ViewBag.Message
</div>
}
<div class="editor-label">
@Html.LabelFor(model => model.HEI_ID)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.HEI_ID)
@Html.ValidationMessageFor(model => model.HEI_ID)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.Username)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Username)
@Html.ValidationMessageFor(model => model.Username)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.Title)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Title)
@Html.ValidationMessageFor(model => model.Title)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.Firstname)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Firstname)
@Html.ValidationMessageFor(model => model.Firstname)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.Lastname)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Lastname)
@Html.ValidationMessageFor(model => model.Lastname)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.Email)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Email)
@Html.ValidationMessageFor(model => model.Email)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.Direct_Number)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Direct_Number)
@Html.ValidationMessageFor(model => model.Direct_Number)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.Mobile_Number)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Mobile_Number)
@Html.ValidationMessageFor(model => model.Mobile_Number)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.Password)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Password)
@Html.ValidationMessageFor(model => model.Password)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.Re_Enter_Password)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Re_Enter_Password)
@Html.ValidationMessageFor(model => model.Re_Enter_Password)
</div>
<p>
<input type="submit" value="Create" />
</p>
</fieldset>
}
<div>
@Html.ActionLink("Back to List", "Index")
</div>
@section Scripts {
@Scripts.Render("~/bundles/jqueryval")
}
那么,在你的捕获中,你抛出了错误,错误的细节是什么?@DavidG这是我得到的错误这是我得到的错误