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 ...
随机推荐
- zookeeper-3.5.5安装报错:找不到或无法加载主类 org.apache.zookeeper.server.quorum.QuorumPeerMain
版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明.本文链接:https://blog.csdn.net/jiangxiulilinux/artic ...
- Build Telemetry for Distributed Services之OpenTracing实践
官网:https://opentracing.io/docs/best-practices/ Best Practices This page aims to illustrate common us ...
- JavaFX入门:简单Demo-学习NetBeans开发平台
零. 最终目标 通过两种方式(纯代码控制.FXML),实现一个简单的登录界面: Paste_Image.png 涉及到的控件: 文本(Text,动态显示内容).标签(Label,显示文本).文本域 ...
- PAT 甲级 1017 Queueing at Bank (25 分)(模拟题,有点思维小技巧,第二次做才理清思路)
1017 Queueing at Bank (25 分) Suppose a bank has K windows open for service. There is a yellow line ...
- JAVA-开发构建Gradle项目安装使用教程
一.简介: Gradle是一个基于Apache Ant和Apache Maven概念的项目自动化构建开源工具.它使用一种基于Groovy的特定领域语言(DSL)来声明项目设置,目前也增加了基于Kotl ...
- react中 如何异步展示后台接口的提示消息
调用接口后,后台会返回这样的一段信息提示:{"errCode":400002,"errMsg":"字段校验异常","data&qu ...
- iOS- UITextView禁止Emoji表情
UITextView代理方法:判断 -(void)textViewDidEndEditing:(UITextView *)textView{ if ([self stringContai ...
- Flutter 拖拽控件Draggable
Flutter提供了强大的拖拽控件,可以灵活定制,并且非常简单.下面作一个拖拽的案例. Draggable Widget Draggable控件负责就是拖拽,父层使用了Draggable,它的子元素就 ...
- zepto手机拼音字母城市选择器代码
<!doctype html> <html> <head> <meta http-equiv="Content-Type" content ...
- POJ 3274/洛谷 1360:Gold Balanced Lineup 黄金阵容平衡
题目描述 Farmer John's N cows (1 ≤ N ≤ 100,000) share many similarities. In fact, FJ has been able to na ...