MVC过滤器基本使用

Action过滤器

/// <summary>
/// 执行代码前执行
/// </summary>
/// <param name="filterContext"></param>
public override void OnActionExecuting(ActionExecutingContext filterContext)
{
  //1.显示触发过滤器action方法的名称
  string actionName = filterContext.ActionDescriptor.ActionName;
   filterContext.HttpContext.Response.Write("ActionName=" + actionName + "</br>");
  //2.显示触发过滤器action方法所在的控制器
  string ctrlName = filterContext.ActionDescriptor.ControllerDescriptor.ControllerName;
  filterContext.HttpContext.Response.Write("ControllerName=" + ctrlName + "</br>");
  //3.判断当前action方法是否贴有[HttpGet]特性标签
  bool isTrue = filterContext.ActionDescriptor.IsDefined(typeof(HttpGetAttribute), false);
  filterContext.HttpContext.Response.Write("当前action是否贴有[httpGet]特性" + isTrue + "</br>");
  //4.获取当前action方法上贴有[HttpGet]特性标签的实例
  object[] httpGets = filterContext.ActionDescriptor.GetCustomAttributes(typeof(HttpGetAttribute), false);
  foreach (object obj in httpGets)
  {
  filterContext.HttpContext.Response.Write("获取当前action方法上贴有[HttpGet]特性标签的实例:" + obj.ToString() + "</br>");
  }
  //5.记录当前action方法的实际参数
  ParameterDescriptor[] dics = filterContext.ActionDescriptor.GetParameters();
  foreach(ParameterDescriptor dic in dics)
  {
  //打印action参数名称
  filterContext.HttpContext.Response.Write("记录当前action方法的实际参数:" + dic.ParameterName.ToString() + "</br>");
  }
  base.OnActionExecuting(filterContext);
}
/// <summary>
/// 执行代码后执行
/// </summary>
/// <param name="filterContext"></param>
public override void OnActionExecuted(ActionExecutedContext filterContext)
{
  base.OnActionExecuted(filterContext);
}
/// <summary>
/// 返回结果前执行
/// </summary>
/// <param name="filterContext"></param>
public override void OnResultExecuting(ResultExecutingContext filterContext)
{
    base.OnResultExecuting(filterContext);
}
/// <summary>
/// 返回结果后执行
/// </summary>
/// <param name="filterContext"></param>
public override void OnResultExecuted(ResultExecutedContext filterContext)
{
    base.OnResultExecuted(filterContext);
}

 

异常过滤器:HandleErrorAttribute

public override void OnException(ExceptionContext filterContext)
{
    //1.获取异常对象
    Exception ex= filterContext.Exception;
    //2.将异常写入日志
    //2.1写入记录日志的具体实现过程略...
    //3.告诉mvc框架,过滤器已处理异常,不需要额外处理
    filterContext.ExceptionHandled = true;
    base.OnException(filterContext);
}

 

配置错误页:

1 <customErrors mode="On" defaultRedirect="Error">
2     <error statusCode="404" redirect="~/ErrorMgr/P404" />
3     <error statusCode="500" redirect="~/ErrorMgr/P500"/>
4 </customErrors>

MVC过滤器基本使用,布布扣,bubuko.com

时间: 2024-08-09 23:49:39

MVC过滤器基本使用的相关文章

ASP.NET MVC过滤器(一)

MVC过滤器是加在 Controller 或 Action 上的一种 Attribute,通过过滤器,MVC 网站在处理用户请求时,可以处理一些附加的操作,如:用户权限验证.系统日志.异常处理.缓存等.MVC 中包含Authorization filter.Action filter.Result filter.Exception filter 四种过滤器. APS.NET MVC中的每一个请求,都会分配给相应的控制器和对应的行为方法去处理,而在这些处理的前前后后如果想再加一些额外的逻辑处理.这

MVC过滤器详解和示例

原文  http://blog.csdn.net/ankeyuan/article/details/29624005 MVC过滤器一共分为四个:ActionFilter(方法过滤器),ResultFilter(结果过滤器,感觉不是很好听,就这样叫吧),AuthorizationFilter(授权过滤器),ExceptionFilter(异常处理过滤器) 过滤器类型 接口 默认实现 描述 Action IActionFilter ActionFilterAttribute 在动作方法之前及之后运行

ASP.NET MVC 过滤器说明

ASP.NET MVC 过滤器分别如下: 过滤器执行顺序:  

ASP.NET MVC 过滤器(三)

ASP.NET MVC 过滤器(三) 前言 本篇讲解行为过滤器的执行过程,过滤器实现.使用方式有AOP的意思,可以通过学习了解过滤器在框架中的执行过程从而获得一些AOP方面的知识(在顺序执行的过程中,这种编程模式就是横向的插入点),言归正传,我们还是以学习过滤器为主.对于IAuthorizationFilter授权认证过滤器的使用篇幅,我知道怎么用但是写不出来,里面包含知识点很多,功底尚浅写了一半又给删掉了,宁愿不发也不能坑人,在后面的学习中假使我可以掌握了,一定会及时的写出来跟大家分享.这个目

ASP.NET MVC 过滤器(一)

ASP.NET MVC 过滤器(一) 前言 前面的篇幅中,了解到了控制器的生成的过程以及在生成的过程中的各种注入点,按照常理来说篇幅应该到了讲解控制器内部的执行过程以及模型绑定.验证这些知识了.但是呢,在MVC框架中提供了一种机制在控制器方法执行之前我们还可以通过这种机制来做一些横向切面的操作,这种机制的实现就是过滤器了,在本篇和后续的篇幅中将会对几种过滤器做一番讲解,并且会对过滤器在框架中的一个执行过程进行粗略的讲解. ASP.NET MVC过滤器 过滤器在系统框架中的整体对象模型 IAuth

ASP.NET MVC 过滤器(四)

ASP.NET MVC 过滤器(四) 前言 前一篇对IActionFilter方法执行过滤器在框架中的执行过程做了大概的描述,本篇将会对IActionFilter类型的过滤器使用来做一些介绍. ASP.NET MVC过滤器 过滤器在系统框架中的整体对象模型 IAuthorizationFilter授权认证过滤器的执行过程 使用IAuthorizationFilter过滤器 IActionFilter行为过滤器的执行过程 自定义实现IActionFilter行为过滤器 异常过滤器的使用 自定义实现

MVC过滤器进行统一登录验证

统一登录验证: 1.定义实体类Person:利用特性标签验证输入合法性设计登录页面 1 2 3 4 5 6 7 8 9 public class Person {     [DisplayName("用户名"), Required(ErrorMessage = "账户非空!")]     public string LoginName { get; set; }     [DisplayName("密 码"), Required(ErrorMes

ASP.NET MVC 过滤器开发与使用

文章来源:http://www.cnblogs.com/JinvidLiang/p/4660200.html(感谢) ASP.NET MVC 过滤器开发与使用 ASP.NET MVC 中给我们提供了内置的过滤器,通过过滤器,我们可以在控制器内的方法前后,添加必须的业务逻辑,如权限验证,身份验证,错误处理等. 今天,我们主要介绍3个过滤器:OutputCacheAttribute,AuthorizeAttribute,HandleErrorAttribute. 我们会根据这三个内置过滤器,分别举不

MVC过滤器 OnActionExecuting() 在过滤器中获取触发控制器,Action 等

<1> using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Mvc; namespace MVC过滤器.Filters { //自定义一个过滤器 [MyActionFilter] public class MyActionFilterAttribute:ActionFilterAttribute { //重写OnActionExecuting方