MVC全局过滤器
Asp.NET MVC4中的全局过滤器,可以对整个项目进行全局监控。
新建一个MVC4项目,可以在global.asax文件中看到如下代码: FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
表示注册全局过滤器.
GlobalFilters是全局过滤器的集合,可以通过add方法添加过滤器,默认情况下,HandleErrorAttribute过滤器被添加到集合中。
接下来我们创建一个自定义过滤器,然后添加到全局过滤器集合中。
1.创建自定义过滤器
创建自定义过滤器要继承ActionFilterAttribute类。我们创建一个名称为CustomerFilterAttribute的过滤器,在action里面记录下时间。
代码如下:
public class CustomerFilterAttribute : ActionFilterAttribute
{
public override void OnActionExecuting(ActionExecutingContext filterContext)
{
base.OnActionExecuting(filterContext);
filterContext.HttpContext.Response.Write("开始时间:" + DateTime.Now.ToString() + "<br/>");
}
public override void OnActionExecuted(ActionExecutedContext filterContext)
{
base.OnActionExecuted(filterContext);
var controllerName = filterContext.RouteData.Values["controller"].ToString();
var actionName = filterContext.RouteData.Values["action"].ToString();
filterContext.HttpContext.Response.Write("结束时间:" + DateTime.Now.ToString() + "<br/>");
filterContext.HttpContext.Response.Write("controller:" + controllerName + ",action:" + actionName);
}
}
2.注册全局过滤器
过滤器创建完成后,我们把这个过滤器添加到全局过滤器中,使用 filters.Add(new CustomerFilterAttribute());方法,
代码如下:
public class FilterConfig
{
public static void RegisterGlobalFilters(GlobalFilterCollection filters)
{
filters.Add(new HandleErrorAttribute());
filters.Add(new CustomerFilterAttribute());
}
}
接下来我们运行项目中的每一个页面,都会看到页面中输出时间和controller名称,效果图如下:
本文内容来源:http://blog.csdn.net/zx13525079024/article/details/19161777
MVC全局过滤器的更多相关文章
- Mvc全局过滤器与Action排除
http://blog.csdn.net/shuaihj/article/details/53020428 如何一次性给所有action做登录验证过滤,如何排除不需要做登录验证的action? 1. ...
- mvc全局过滤器和httpmodule的执行顺序
根据http管线模型,请求先通过httpmodule,再通过httphandler,之后再进入mvc的过滤器 另外参考:MVC如何在Pipeline中接管请求的? http://www.cnblogs ...
- .Net Core MVC全局过滤器验证是否需要登录
1.新增全局登录过滤器LoginCheckAttribute 1 public class LoginCheckAttribute: ActionFilterAttribute 2 { 3 publi ...
- ASP.NET MVC 全局过滤器(FilterConfig)、标记在控制器上和方法上的筛选器执行顺序
FilterConfig->控制器上的筛选器-->方法上的筛选器(大-->小,上-->下) 全局-->控制器->个别 尝试的时候记得把返回true protecte ...
- MVC 全局过滤器
1. 新创建一个类 CheckLogin2. 在类中加入以下代码 public class CheckLogin : ActionFilterAttribute { public override v ...
- MVC 全局拦截aciton
上篇:MVC 拦截指定的action 有时,我们需要对所有aciton在执行前/后做一些(预)处理,如何实现呢? 1.定义一个action筛选类.继承至System.Web.Mvc.IActionFi ...
- MVC中使用Action全局过滤器出现:网页无法正常运作 将您重定向的次数过多。解决办法
前言当我们访问某个网站的时候需要检测用户是否已经登录(通过Session是否为null),我们知道在WebForm中可以定义一个BasePage类让他继承System.Web.UI.Page,重写它的 ...
- MVC下用户登录状态校验的问题以及解决方案--------------Action全局过滤器的使用
前言当我们访问某个网站的时候需要检测用户是否已经登录(通过Session是否为null),我们知道在WebForm中可以定义一个BasePage类让他继承System.Web.UI.Page,重写它的 ...
- MVC 全局异常过滤器HandleErrorAttribute
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.We ...
随机推荐
- numpy中flatten学习笔记
ndarray.flatten() 用法 用于返回一个折叠成一维的数组.该函数只能适用于numpy对象,即array或者mat,普通的list列表是不行的. 例子 # coding=utf-8 fro ...
- 5.性能测试工具比较:Jmeter和LR
性能测试工具较多,无法一一进行介绍,感兴趣者可自行搜索资料学习.需要说明的是工具使用方法和原理都大同小异,掌握一个,其他皆可快速上手. 下面就以服务端的性能测试工具为例,对市场上最常用,知名度较高,也 ...
- Kinect关节数据
3 -0.118269 0.655295 1.7431 930.03 139.5962 -0.124249 0.506111 1.79473 926.387 239.42820 -0.122777 0 ...
- postgreSQL 之 Privilege & grant & revoke(未完待续)
When an object is created, it is assigned an owner. The owner is normally the role that executed the ...
- zip炸弹
故障系统有人提了zip炸弹的故障,了解了一些关于zip炸弹的常识. 42.zip 是很有名的zip炸弹.一个42KB的文件,解压完其实是个4.5PB的“炸弹”. 更有甚者,一个叫做 droste.zi ...
- PAT 甲级 1026 Table Tennis (30 分)(坑点很多,逻辑较复杂,做了1天)
1026 Table Tennis (30 分) A table tennis club has N tables available to the public. The tables are ...
- LeetCode_13. Roman to Integer
13. Roman to Integer Easy Roman numerals are represented by seven different symbols: I, V, X, L, C, ...
- JAVA 基础编程练习题49 【程序 49 子串出现的个数】
49 [程序 49 子串出现的个数] 题目:计算字符串中子串出现的次数 package cskaoyan; public class cskaoyan49 { public static void m ...
- MySQL问题:Access denied for user 'mysql'@'localhost'
deep@deep-PC:~$ sudo mysql -uroot -p mysql> update mysql.user set authentication_string=PASSWORD( ...
- 3rd.botan
1.HOME 1.官网:https://botan.randombit.net/ Win下 编译步骤:https://botan.randombit.net/handbook/building.htm ...