zoukankan      html  css  js  c++  java
  • 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 />发生异常,可以写日志了");  
            }
        }
    

      

  • 相关阅读:
    windows服务启动有界面的程序
    [转发]读取txt防止读到乱码--自动根据文件编码进行读取
    线程UI同步
    SQL2012导出的脚本没有if exists判断
    power designer 设计数据库生成到oracle数据库
    经典三层,单元工作模式出错的解决方案
    EF ObjectStateManager 中已存在具有同一键的对象。ObjectStateManager 无法跟踪具有相同键的多个对象
    Oracle 11g 客户端连接 oracle 10g 服务端,乱码问题
    EF 连接oracle 基础连接失败 问题解决
    vs2010 oraclelient 引用问题
  • 原文地址:https://www.cnblogs.com/ideacore/p/6862525.html
Copyright © 2011-2022 走看看