MVC过滤器的详细讲解和示范样本
MVC共有4个过滤器:ActionFilter(方法过滤器),ResultFilter(结果过滤器。感觉是不是很好,所以称它为),AuthorizationFilter(授权过滤器)。ExceptionFilter(异常处理过滤器)
| 过滤器类型 | 接口 | 默认实现 | 描写叙述 |
| Action | IActionFilter | ActionFilterAttribute | 在动作方法之前及之后执行 |
| Result | IResultFilter | ActionFilterAttribute | 在动作结果被执行之前和之后执行 |
| AuthorizationFilter | IAuthorizationFilter | AuthorizeAttribute | 首先执行,在不论什么其他过滤器动作方法之前执行 |
| Exception | IExceptionFilter | HandleErrorAttribute | 仅仅在另外一个过滤器,动作方法,动作结果弹出异常时执行 |
演示样例:Action方法过滤器
/// <summary>
/// Action方法 过滤器 类
/// </summary>
public class MyActionFilterAttribute : ActionFilterAttribute
{
/// <summary>
/// 在 Action方法之前 调用
/// </summary>
/// <param name="filterContext"></param>
public override void OnActionExecuting(ActionExecutingContext filterContext)
{
//1. RouteData 中 保存了 当前请求 匹配的 路由信息和路由对象
// 假设本次请求 是请求了某个 区域 里的 控制器方法,还能够通过filterContext.RouteData.DataTokens["area"]获取区域名 //string strArea = filterContext.RouteData.DataTokens["area"].ToString();
string strController = filterContext.RouteData.Values["controller"].ToString();
string strAction = filterContext.RouteData.Values["action"].ToString();
//filterContext.RouteData.GetRequiredString //2.还有一种方式 获取 请求的 类名和方法名
string strAction2 = filterContext.ActionDescriptor.ActionName;
string strController2 = filterContext.ActionDescriptor.ControllerDescriptor.ControllerName; //2.1检查 被请求方法 是否 加了 MoneyAttribute 特性
if (filterContext.ActionDescriptor.IsDefined(typeof(Filters.MoneyAttribute), false))
{
//直接为 请求 设置 返回结果。而不运行 相应的 Action 方法,也不运行 OnActionExcuted,可是。会运行 Result过滤器和 生成视图
filterContext.Result = new ContentResult() { Content = "<br/>哈哈哈。直接被跳过了吧~~~!<br/>" };
} filterContext.HttpContext.Response.Write("哇哈哈哈~!OnActionExecuting<br/>");
base.OnActionExecuting(filterContext);
} /// <summary>
/// 在 Action方法之后 调用
/// </summary>
/// <param name="filterContext"></param>
public override void OnActionExecuted(ActionExecutedContext filterContext)
{
filterContext.HttpContext.Response.Write("哇哈哈哈~。OnActionExecuted<br/>");
base.OnActionExecuted(filterContext);
}
用法1:将过滤器加到方法上
[Filters.MyActionFilter]//1.将 过滤器 加到方法上
[Filters.Money]
[Filters.MyResultFilter]
[Filters.MyAuthorize]
public ActionResult Index()
{
Response.Write("Index 方法<br/>");
ViewBag.name = "时间:" + DateTime.Now;
return View();
}
用法2:将过滤器加到方类上
[Filters.MyActionFilter]
public class HomeController : Controller
{
}
用法3:加入全局过滤器,即加入到FilterConfig.cs中
public class FilterConfig
{
public static void RegisterGlobalFilters(GlobalFilterCollection filters)
{
//filters.Add(new HandleErrorAttribute()); //3. 加入全局过滤器
filters.Add(new Filters.MyActionFilterAttribute()); filters.Add(new Filters.MyHandleErrorAttribute());
}
}
演示样例:Result过滤器
/// <summary>
/// Result 过滤器 类 - 假设请求的是 要载入视图的 Action 方法的话
/// 在 视图载入 前 后 调用方法
/// </summary>
public class MyResultFilterAttribute:System.Web.Mvc.ActionFilterAttribute
{
/// <summary>
/// 载入 "视图" 前运行
/// </summary>
/// <param name="filterContext"></param>
public override void OnResultExecuting(System.Web.Mvc.ResultExecutingContext filterContext)
{
filterContext.HttpContext.Response.Write("载入 视图 前运行 OnResultExecuting <br/>");
base.OnResultExecuting(filterContext);
} /// <summary>
/// 载入"视图" 后运行
/// </summary>
/// <param name="filterContext"></param>
public override void OnResultExecuted(System.Web.Mvc.ResultExecutedContext filterContext)
{
filterContext.HttpContext.Response.Write("载入 视图 后运行 OnResultExecuted <br/>");
base.OnResultExecuted(filterContext);
}
}
演示样例:授权过滤器 - 在 Action过滤器前 运行
/// <summary>
/// 授权过滤器 - 在 Action过滤器前 运行
/// </summary>
public class MyAuthorizeAttribute:AuthorizeAttribute
{
public override void OnAuthorization(AuthorizationContext filterContext)
{
filterContext.HttpContext.Response.Write("<br/>OnAuthorization<br/>");
//凝视掉 父类方法,由于 父类里的 OnAuthorization 方法会 调用 asp.net的授权验证机制!
//base.OnAuthorization(filterContext);
}
}
演示样例:Exception过滤器
/// <summary>
/// 异常处理 过滤器
/// </summary>
public class MyHandleErrorAttribute : HandleErrorAttribute
{
public override void OnException(ExceptionContext filterContext)
{
//1.获取异常对象
Exception ex = filterContext.Exception;
//2.记录异常日志
//3.重定向友好页面
filterContext.Result = new RedirectResult("~/error.html");
//4.标记异常已经处理完成
filterContext.ExceptionHandled = true; base.OnException(filterContext);
}
}
版权声明:本文博主原创文章,博客,未经同意不得转载。
MVC过滤器的详细讲解和示范样本的更多相关文章
- Spring MVC详细讲解
一:三层架构和MVC 1:三层架构 我们的开发架构一般都是基于两种形式:一种是 C/S 架构,也就是客户端/服务器,另一种是 B/S 架构,也就是浏览器服务器.在 JavaEE 开发中,几乎全都是基于 ...
- ASP.NET MVC 过滤器(一)
ASP.NET MVC 过滤器(一) 前言 前面的篇幅中,了解到了控制器的生成的过程以及在生成的过程中的各种注入点,按照常理来说篇幅应该到了讲解控制器内部的执行过程以及模型绑定.验证这些知识了.但是呢 ...
- ASP.NET MVC 过滤器(五)
ASP.NET MVC 过滤器(五) 前言 上篇对了行为过滤器的使用做了讲解,如果在控制器行为的执行中遇到了异常怎么办呢?没关系,还好框架给我们提供了异常过滤器,在本篇中将会对异常过滤器的使用做一个大 ...
- ASP.NET MVC 过滤器详解
http://www.fwqtg.net/asp-net-mvc-%E8%BF%87%E6%BB%A4%E5%99%A8%E8%AF%A6%E8%A7%A3.html 我经历了过滤器的苦难,我想到了还 ...
- MVC 过滤器1
ASP.NET MVC 过滤器(一) 前言 前面的篇幅中,了解到了控制器的生成的过程以及在生成的过程中的各种注入点,按照常理来说篇幅应该到了讲解控制器内部的执行过程以及模型绑定.验证这些知识了.但是呢 ...
- ASP.NET MVC 过滤器(三)
ASP.NET MVC 过滤器(三) 前言 本篇讲解行为过滤器的执行过程,过滤器实现.使用方式有AOP的意思,可以通过学习了解过滤器在框架中的执行过程从而获得一些AOP方面的知识(在顺序执行的过程中, ...
- ASP.NET MVC 过滤器(四)
ASP.NET MVC 过滤器(四) 前言 前一篇对IActionFilter方法执行过滤器在框架中的执行过程做了大概的描述,本篇将会对IActionFilter类型的过滤器使用来做一些介绍. ASP ...
- MVC过滤器详解
MVC过滤器详解 APS.NET MVC中(以下简称"MVC")的每一个请求,都会分配给相应的控制器和对应的行为方法去处理,而在这些处理的前前后后如果想再加一些额外的逻辑处理. ...
- MVC过滤器使用案例:统一处理异常顺道精简代码
重构的乐趣在于精简代码,模块化设计,解耦功能……而对异常处理的重构则刚好满足上述三个方面,下面是我的一点小心得. 一.相关的学习 在文章<精简自己20%的代码>中,讨论了异常的统一处理,并 ...
随机推荐
- IOS中的id与nil
1 id id和void *并非完全一样.在上面的代码中,id是指向struct objc_object的一个指针,这个意思基本上是说,id是一个指向任何一个继承了Object(或者NSObject) ...
- Linux中利用crontab创建计划任务
在linux中启动crontab服务: /etc/init.d/crond start crontab的命令格式 crontab -l 显示当前的crontab 文件(默认编写的crontab文 ...
- 对Kalman(卡尔曼)滤波器的理解
1.简单介绍(Brief Introduction) 在学习卡尔曼滤波器之前,首先看看为什么叫"卡尔曼". 跟其它著名的理论(比如傅立叶变换.泰勒级数等等)一样.卡尔曼也是一个人的 ...
- 【STL】关联容器 — hash_set
容器hash_set是以hash table为底层机制的,差点儿所有的操作都是转调用hash table提供的接口.因为插入无法存储同样的键值,所以hash_set的插入操作所有都使用hash tab ...
- android 防止多次点击,它会导致事件侦听响应于其他接口
这里有情况: A当点击跳转至B介面,点击B接口结束后,到A界面中 1.此时在B界面中,设置点击事件,点击后结束B v.setOnClickListener(new OnClickListener() ...
- JDBC数据库编程常用接口(转)
JDBC的全称是Java DataBase Connectivity,是一套面向对象的应用程序接口(API),制定了统一的访问各种关系数据库的标准接口,为各个数据库厂商提供了标准接口的实现.这东西能够 ...
- Lua 与C 交换 第一篇
编译 windows上编译lua源代码 cl /MD /O2 /W3 /c /DLUA_BUILD_AS_DLL *.c del *.o ren lua.obj lua.o ren luac.obj ...
- Android它Service
服务是一段代码的后台执行. 无法处理,也不是螺纹,但它是在进程和线程的执行. Android该服务与Activity不同,不能与用户交互,无法启动自己. 媒体播放服务.当用户退出媒体选择用户界面,不过 ...
- Windows phone 8 学习笔记(9) 集成
原文:Windows phone 8 学习笔记(9) 集成 本节整理了之前并没有提到的Windows phone 8 系统相关集成支持,包括选择器.锁定屏幕的.联系人的访问等.选择器列举了若干内置应用 ...
- HUNNU11352:Digit Solitaire
Problem description Despite the glorious fall colors in the midwest, there is a great deal of time t ...