许多 Web 应用程序要求在用户登陆以后才授予其对受限制内容的访问权限。html
在某些应用程序中,即便是登陆的用户,也会限制他们能够查看的内容或能够编辑的字段。jquery
要限制对 ASP.NET MVC 视图的访问,您能够限制对呈现视图的操做方法的访问。web
为此,MVC 框架提供 AuthorizeAttribute 类。ajax
MVC的一些特性,以下:数据库
BindAttribute(限制实体属性)
RemoteAttribute(远程验证,须要页面使用jquery.validate.js和jquery.validate.unobtrusive.js)
HandleErrorAttribute(根据异常类型直接跳转到相应的错误页面)
HiddenInputAttribute(在Model中直接控制页面输入框显示,用处不大)
使用BindAttribute的目的是限制用户在提交form表单时使用合适且正确的值。当咱们提交一个表单时,就会检查每个实体上绑定的特性。框架
假设咱们已经有下面一个Employee实体类:异步
public class Employee { public string Name { get; set; } public string Email { get; set; } public string Address { get; set; } public string PhoneNo { get; set; } }
创建一个EmployeeController,里面添加两个Action:ide
[HttpGet] public ActionResult EmployeeRegister() { return View(); } [HttpPost] public ActionResult EmployeeRegister(Employee emp) { return View(); }
给第一个Action创建视图,而且发送post表单数据过来。post
这样在第二个Action中,就会接收到参数,自动转成Employee类,包含4个属性spa
如今若是咱们只想提交Email,Name和PhoneNo,而咱们不想提交Address,这时咱们能够在实体类上添加以下特性:
[Bind(Exclude="Address")] public class Employee { public string Name { get; set; } public string Email { get; set; } public string Address { get; set; } public string PhoneNo { get; set; } }
此时其余三个属性正常,Address变成了null.
咱们也能够将BindAttribute直接用在Action的参数中,像下面这样:
[HttpPost] public ActionResult EmployeeRegister([Bind(Exclude = "Address")]Employee emp) { return View(); }
注意:BindAttribute位于System.Web.Mvc命名空间下
假设咱们有一个注册表单,里面有邮箱文本框,当输入邮箱后,咱们想检查输入的邮箱是否在数据库中已经存在,若是存在,则不提交表单,这时咱们可使用RemoteAttribute,经过RemoteAttribute,咱们能够在进入Action前自动先进行一些服务端验证。
咱们能够在下面的例子中使用RemoteAttribute:
public class Employee { public string Name { get; set; } [Remote("CheckEmail","Employee",ErrorMessage="Email is already exist")] public string Email { get; set; } public string Address { get; set; } public string PhoneNo { get; set; } }
RemoteAttribute的第一个参数是一个Action名字,第二个是Controller名字,第三个是若是邮箱已存在后显示给用户看的提示信息。当咱们输入完邮箱后,CheckEmail方法将被执行并检查邮箱是否存在。
public JsonResult CheckEmail(string Email) { //Check here in database if it exist in database return true else false. return Json(false, JsonRequestBehavior.AllowGet); }
此时页面上邮箱的输入框一旦发生onchange,就会自动发送异步请求到CheckEmail方法,页面可使用以下代码:
@Html.ValidationMessageFor(model => model.Email, "", new { @class = "text-danger" })
来配合处理
我的认为,该特性基本没什么用
咱们已经有不少方法在MVC中处理异常,好比用try catch,或者使用Filter,或者经过第三方库好比elmah。可是MVC也提供了一个HandleErrorAttribute去处理异常,以下:
[HandleError()] public ActionResult CheckError() { int a = 10; int b = 0; int k = a / b; return View(); }
在web.config文件中,咱们在<system.web>中添加以下两行:
<customErrors mode ="On" defaultRedirect ="Error.cshtml"> </customErrors>
在shared文件夹下建立一个视图Error.cshtml,而后运行程序,若是运行上面的CheckError()方法,刚建立的Error.cshtml将会显示出来。
还能够根据异常类型的不一样跳转到不一样的错误界面。
[HandleError(ExceptionType=typeof(DivideByZeroException),View="尝试除以0的View")] [HandleError(ExceptionType = typeof(NullReferenceException), View = "引用null对象的View")] public ActionResult CheckError() { int a = 10; int b = 0; int k = a / b; return View(); }
使用 handleError attribute 有如下局限:
1. 不支持exception记录
2. 没法捕捉到500以外的http exception
3. controller以外抛出的异常没法处理
4. ajax调用出现exception时,会将错误页面内容返回
若是咱们想对用户隐藏一些实体字段,咱们可使用HiddenInput特性。
public class Employee { [HiddenInput(DisplayValue=false)] public string Name { get; set; } [Remote("CheckEmail","Employee",ErrorMessage="Email is already exist")] public string Email { get; set; } public string Address { get; set; } public string PhoneNo { get; set; } }
这样页面上Name的输入框将不会出现,必须使用以下代码才可实现控制效果:
@Html.EditorFor(model => model.Name, new {})
强类型控制,Name和页面头部引用@model xxx.Models.Employee必须保持一致