Attribute自定义特性+Asp.net MVC中的filter详解

转载自:http://blog.csdn.net/wangyy130/article/details/44241957

一、filter简介

         在了解自定义特性前,先引入一个概念filter,它是MVC中自带的一种功能,在我们项目中通常会遇到在Action执行前或结束时,去执行日志记录或错误处理等功能,通常可使用AOP截取来实现,但是在MVC中提供了filter过滤,大大方便了开发人员。

 

MVC中的filter类型:

二、应用

声明一个自定义特性,继承自ActionFilterAttribute

具体代码:

//[AttributeUsage (AttributeTargets.All ,AllowMultiple =true)]//allmultiple容许多个标签同时起作用  
    public class MyActionfilter:ActionFilterAttribute   
    {  
       public  string Name { set; get; }  
       
        //action执行之前先执行此方法  
        public override void OnActionExecuting(ActionExecutingContext filterContext)  
        {  
            base.OnActionExecuting(filterContext);  
            HttpContext.Current.Response.Write("<br />OnOnActionExecuting:" + Name);  
        }  
  
         //action执行之后先执行此方法  
        public override void OnActionExecuted(ActionExecutedContext filterContext)  
        {  
            base.OnActionExecuted(filterContext);  
            HttpContext.Current.Response.Write("<br />onActionExecuted:" + Name);  
        }  
        //actionresult执行之前执行此方法  
        public override void OnResultExecuting(ResultExecutingContext filterContext)  
        {  
            base.OnResultExecuting(filterContext);  
            HttpContext.Current.Response.Write("<br />OnResultExecuting:" + Name);  
  
        }  
  
         //actionresult执行之后执行此方法  
        public override void OnResultExecuted(ResultExecutedContext filterContext)  
        {  
            base.OnResultExecuted(filterContext);  
            HttpContext.Current.Response.Write("<br />OnResultExecuted:" + Name);  
  
        }  
    }  

  使用

[MyActionfilter(Name="IndexAction")]
        public ActionResult Index()
        {
           Response.Write("<p>action被执行完了</p>");
            return Content("<br/>ok:视图被渲染了!<br/>");
        }

执行上述代码结果:

三、filter优先级别

如上所述,controller中的只有Index方法中有自定义特性,如果想让所有的Action在执行时,都进行过滤,那么我们可以在Controller上添加自定义filter特性标签,这样执行它的范围就是整个Controller

 

而如果我们想要在所有的Controller中的所有Action中均执行此方法呢?我们可以在App_Start中的filterConfig中对自定义的过滤器进行注册

Filters.Add(newMyActionFilterAttribute(){Name="Global"});//全局过滤

 

 

那么这样的话就产生了优先级问题,离自己最近的优先级别最高,方法级别>Controller级别>全局级别

 

那么如果我想让所有级别的方法均生效,就是每个级别的特性方法都去执行一遍,那么又该怎样呢?这里就用到了AttributeUsage这个类了

将 MyActionfilter 上面注掉的解开

//[AttributeUsage (AttributeTargets.All ,AllowMultiple =true)]//allmultiple容许多个标签同时起作用

AllowMultiple这个属性的值设为true,此时便会执行所有声明的特性方法了。

 

总结:通过以上对filter的使用,应该对自定义特性有了一个初步的了解,同时在项目中UI中用到的自定义特性,通过反射来解析,同时在处理异常时,我们可以利用异常特性HandleErrorAttribute来对程序中出现的异常进行处理,微软默认在全局过滤器中加上了处理异常过滤,但是我们也可以加上自己的异常过滤。再者,MVC中自带的前端UI校验用的其实也是特性的相关实现。更多关于特性的知识有待我们进一步探索。

 

另外用得多的Filter可能就是ExceptionFilter了  比如发生异常写日志啊啥的

MVC会自己实现一个HandleErrorAttribute 并且在 FilterConfig.cs 设置为全局的,所以如果自己需要自定义一个ExceptionFilter可以继承 HandleErrorAttribute 然后重写其中的 OnException 

[AttributeUsage(AttributeTargets.All, AllowMultiple = true)]//allmultiple容许多个标签同时起作用  
    public class MyExceptionFilter : HandleErrorAttribute
    {
        public override void OnException(ExceptionContext filterContext)
        {
            
            base.OnException(filterContext);
            HttpContext.Current.Response.Redirect("http://www.baidu.com");
            //HttpContext.Current.Response.Write("<br />发生异常,可以写日志了");  
        }
    }

  

posted @ 2017-05-16 17:36  上古时期的码农  阅读(2087)  评论(0编辑  收藏  举报