013. MVC5过滤器

微软提供了4中过滤器:

1.Action过滤器: 在Action方法执行之前和Action方法执行之后, 会执行此过滤器中的代码. 比如在执行public ActionResult Index()方法之前或之后; 也可以说是在方法执行前或执行后;

接口: IactionFilter    抽象类名: ActionFilterAttribute   添加一个实现类 MyActionFilterAttribute.cs , 这里这个Attribute后缀必须添加

2. View视图结果渲染过滤器:在视图渲染之前或之后做一点处理;

接口: IresultFilter

3. 全局错误异常过滤器: 当整个网站出现异常, 就来执行此过滤器中代码

接口:

4. 校验过滤器(身份验证过滤器):不常用


 

使用Action过滤器:

添加一个MyActionFilterAttribute类, 继承ActionFilterAttribute  , 一般不去实现接口, 而是去继承这个抽象类, 而这个继承类应该以Attribute作为后缀, 右键Models目录→添加一个MyActionFilterAttribute类:

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace MasterDemo.Models
{
    //如果想要全局的标签都要起到作用, 则需要再在这个类上也打上一个标签, 只要把 AllowMultiple = false, 改成true, 就能使该过滤器全局起到作用, 下面这个仅仅是他的默认特性
    [AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, Inherited = true, AllowMultiple = false)]
    public class MyActionFilterAttribute: System.Web.Mvc.ActionFilterAttribute
    {
        public string Name
        {
            get; set;
        }
        public override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            base.OnActionExecuting(filterContext);
            filterContext.HttpContext.Response.Write("我是执行前打出来的" + Name);
        }

        public override void OnActionExecuted(ActionExecutedContext filterContext)
        {
            base.OnActionExecuted(filterContext);
            filterContext.HttpContext.Response.Write("我是执行后打出来的" + Name);
        }

        public override void OnResultExecuting(ResultExecutingContext filterContext)
        {
            base.OnResultExecuting(filterContext);
            filterContext.HttpContext.Response.Write("我是在结果执行前打出来的" + Name);
        }

        public override void OnResultExecuted(ResultExecutedContext filterContext)
        {
            base.OnResultExecuted(filterContext);
            filterContext.HttpContext.Response.Write("我是结果执行后打出来的" + Name);
        }
    }
}

控制器的引用:

 

运行结果:

Index.cshtml内容:

@{
    ViewBag.Title = "Home Page";
}

<div>
    <h1>ASP.NET</h1>
    
</div>

<div>
        <h2>Getting started</h2>
</div>

 运行结果:

 

打到类上, 此时访问Index页面还是上面的运行结果, 如果访问About页面, 结果则是  我是执行前打出来的Home/我是打在类上的特性; 类上的特性适用于整个类内的所有方法, 但是如果该方法上有了特性, 则以该方法上的特性为主

 全局过滤器:

可以用来实现判断用户是否登录, 验证用户身份

截图:

 

代码:

using System.Web;
using System.Web.Mvc;

namespace MasterDemo
{
    public class FilterConfig
    {
        //要使用全局过滤器, 则必须在此文件中配置全局过滤器, 全局过滤器, 对每一个类都生效, 当然需要将全局过滤器的类打上前面提到的特性, 比如这里要引用/Models/MyActionFilterAttribute的过滤器, 则需要在此添加, 并且/Models/MyActionFilterAttribute类的前面也要添加新的特性, 这个特性在抽象类上其实也是有的
        public static void RegisterGlobalFilters(GlobalFilterCollection filters)
        {
            filters.Add(new HandleErrorAttribute());
            //添加全局过滤器, 此时访问此程序内所有的页面都将先加载此过滤器, 那么这个过滤器可以用来实现判断用户是否登录
            filters.Add(new Models.MyActionFilterAttribute() { Name="我是全局"});
        }
    }
}

 全局过滤器被调用的时间:

看下图, 可以启动下调试, 看看全局过滤器被调用的时间:

 

posted on 2017-02-18 23:18  印子  阅读(445)  评论(0编辑  收藏  举报

导航