过滤器分类:

Action过滤器    View结果渲染过滤器     全局错误异常过滤器    身份验证过滤器

1.Action过滤器:在Action执行之前和执行之后分别干一些事   接口:(IActionFilter)

2.View结果渲染过滤器:   接口:(IActionFilter)

 [AttributeUsage(AttributeTargets.All, AllowMultiple = true, Inherited = true)]
public class MyActionFilterAttribute :ActionFilterAttribute
{
public string Name { get; set; } //在Action执行之前先执行此方法 重写方法 执行之前先执行
public override void OnActionExecuting(ActionExecutingContext filterContext)
{
base.OnActionExecuting(filterContext);
filterContext.HttpContext.Response.Write("<br />OnActionExecuting---"+ Name);
}
//
public override void OnActionExecuted(ActionExecutedContext filterContext)
{
base.OnActionExecuted(filterContext);
filterContext.HttpContext.Response.Write("<br />OnActionExecuted---" + Name);
} public override void OnResultExecuting(ResultExecutingContext filterContext)
{
base.OnResultExecuting(filterContext);
filterContext.HttpContext.Response.Write("<br />OnResultExecuting---" + Name);
} public override void OnResultExecuted(ResultExecutedContext filterContext)
{
base.OnResultExecuted(filterContext);
filterContext.HttpContext.Response.Write("<br />OnResultExecuted---" + Name);
}
}

  怎么去调用呢?

 [MyActionFilter(Name = "Home/Index")]
public ActionResult Index()
{ Response.Write("<br />Action执行了.... <br/>");
}

   注意:类上面的标签可以作用到所有控制器上的方法

所有的控制器都需要的话(全局过滤器):

 public class FilterConfig
{
public static void RegisterGlobalFilters(GlobalFilterCollection filters)
{
filters.Add(new HandleErrorAttribute()); filters.Add(new MyActionFilterAttribute(){Name = "全局的"});
}
}

  调用:

控制器和类中的内容都显示:

 [AttributeUsage(AttributeTargets.All, AllowMultiple = true, Inherited = true)]
public class MyActionFilterAttribute :ActionFilterAttribute
{
}

  

MVC的过滤器的更多相关文章

  1. 使用ASP.NET MVC操作过滤器记录日志(转)

    使用ASP.NET MVC操作过滤器记录日志 原文地址:http://www.singingeels.com/Articles/Logging_with_ASPNET_MVC_Action_Filte ...

  2. 爱上MVC系列~过滤器实现对响应流的处理

    回到目录 MVC的过滤器相信大家都用过,一般用来作权限控制,因为它可以监视你的Action从进入到最后View的渲染,整个过程ActionFilter这个过滤器都参与了,而这给我们的开发带来了更多的好 ...

  3. ASP.NET MVC : Action过滤器(Filtering)

    http://www.cnblogs.com/QLeelulu/archive/2008/03/21/1117092.html ASP.NET MVC : Action过滤器(Filtering) 相 ...

  4. MVC 自定义过滤器/特性来实现登录授权及验证

    最近悟出来一个道理,在这儿分享给大家:学历代表你的过去,能力代表你的现在,学习代表你的将来. 十年河东十年河西,莫欺少年穷 学无止境,精益求精    最近在做自学MVC,遇到的问题很多,索性一点点总结 ...

  5. [翻译] 使用ASP.NET MVC操作过滤器记录日志

    [翻译] 使用ASP.NET MVC操作过滤器记录日志 原文地址:http://www.singingeels.com/Articles/Logging_with_ASPNET_MVC_Action_ ...

  6. MVC 授权过滤器 AuthorizeAttribute

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

  7. MVC自定义过滤器,自定义Area过滤器,自定义Controller,Action甚至是ViewData过滤器

    实现MVC自定义过滤器,自定义Area过滤器,自定义Controller,Action甚至是ViewData过滤器 MVC开发中几种以AOP方式实现的Filters是非常好用的,默认情况下,我们通过A ...

  8. mvc全局过滤器和httpmodule的执行顺序

    根据http管线模型,请求先通过httpmodule,再通过httphandler,之后再进入mvc的过滤器 另外参考:MVC如何在Pipeline中接管请求的? http://www.cnblogs ...

  9. 实现MVC自定义过滤器,自定义Area过滤器,自定义Controller,Action甚至是ViewData过滤器

    MVC开发中几种以AOP方式实现的Filters是非常好用的,默认情况下,我们通过App_Start中的FilterConfig来实现的过滤器注册是全局的,也就是整个应用程序都会使用的,针对单独的Fi ...

  10. ASP.NET MVC动作过滤器

    ASP.NET MVC提供了4种不同的动作过滤器(Aciton Filter). 1.Authorization Filter 在执行任何Filter或Action之前被执行,用于身份验证 2.Act ...

随机推荐

  1. php常用函数汇总

    php常用函数汇总   字符串截取:           1.substr('要截取的字符串','从第几个字符开始','到第几个字符结束');             * 截取英文或者数字       ...

  2. LA 3523 圆桌骑士

    题目链接:http://vjudge.net/contest/141787#problem/A http://poj.org/problem?id=2942 此题很经典 知识点:DFS染色,点-双连通 ...

  3. laravel框架总结(三) -- 路径分析

    1.直接写绝对路径,这样会用在/goods/show前面加上域名 <a href="/goods/show?id=<?php echo $item['id']; ?>&qu ...

  4. centos7 docker mysql56

    yum -y install docker docker pull centos docker run --name=mysqltmp -i -t centos /bin/bash yum -y in ...

  5. ITerm2下使用ssh访问Linux

    通常情况下,iTerm2访问远程Linux使用ssh,与Termial基本一样,方法如下: ssh <用户名>@<ip> 然后输入访问的密码即可.当然还有的时候需要指定访问端口 ...

  6. java实现LIS算法,出操队形问题

    假设有序列:2,1,3,5,求一个最长上升子序列就是2,3,5或者1,3,5,长度都为3. LIS算法的思想是: 设存在序列a. ① 如果只有一个元素,那么最长上升子序列的长度为1: ② 如果有两个元 ...

  7. exception

    SQL> -- 例外 exceptionSQL> -- 1/0SQL> ed已写入 file afiedt.buf 1  declare  2   pnum number := 0; ...

  8. http://www.oreilly.com/catalog/errataunconfirmed.csp?isbn=9780596529321

    集体智慧勘误表: http://www.oreilly.com/catalog/errataunconfirmed.csp?isbn=9780596529321 ------------------- ...

  9. tomcat配置项目的图片路径不在项目下的处理

    <Host appBase="webapps" autoDeploy="true" name="localhost" unpackWA ...

  10. linux笔记:shell编程-正则表达式

    正则表达式与通配符(正则表达式匹配字符串,是包含匹配:通配符匹配文件名,是完全匹配.): 基础正则表达式: 正则表达式示例: