MVC之 自定义过滤器(ActionFilterAttribute)
一、自定义Filter
//表示所有操作-筛选器特性的基类。
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, Inherited = true, AllowMultiple = false)]
public abstract class ActionFilterAttribute : FilterAttribute, IActionFilter, IResultFilter
{
protected ActionFilterAttribute();
// 在Action执行之后由 MVC 框架调用。
public virtual void OnActionExecuted(ActionExecutedContext filterContext);
// 在Action执行之前由 MVC 框架调用。
public virtual void OnActionExecuting(ActionExecutingContext filterContext);
// 在执行Result后由 MVC 框架调用。
public virtual void OnResultExecuted(ResultExecutedContext filterContext);
// 在执行Result之前由 MVC 框架调用。
public virtual void OnResultExecuting(ResultExecutingContext filterContext);
}
因此自定义过滤器可以选择适当的方法来重写方可。下面来举个简单的例子:检查登录状态的过滤器,没有登录则跳转到登录页
[CheckLogin] //此处为自定义属性,要引用相应的命名空间
public ActionResult Index()
{
return View();
} public ActionResult Login() //此Action自动往cookie里写入登录信息
{
HttpCookie hcUserName = new HttpCookie("username","admin");
HttpCookie hcPassWord = new HttpCookie("password","");
System.Web.HttpContext.Current.Response.SetCookie(hcUserName);
System.Web.HttpContext.Current.Response.SetCookie(hcPassWord);
return View();
}
过滤器代码
public class CheckLogin : ActionFilterAttribute
{
//在Action执行之前 乱了点,其实只是判断Cookie用户名密码正不正确而已而已。
public override void OnActionExecuting(ActionExecutingContext filterContext)
{
HttpCookieCollection CookieCollect = System.Web.HttpContext.Current.Request.Cookies;if (CookieCollect["username"] == null || CookieCollect["password"] == null)
{
filterContext.Result = new RedirectResult("/Home/Login");
}
else
{
if (CookieCollect["username"].Value != "admin" && CookieCollect["password"].Value != "")
{
filterContext.Result = new RedirectResult("/Home/Login");
}
}
}
}//本示例贪图方便,将要跳转到的Action放在同一个Controller下了,如果将过滤器放到Controller类顶部,则永远也跳不到这个LoginAction。
此过滤器实现的效果是,当用户Cookie中用户名和密码不正确则跳转到登录页,注意过滤器也可以放在整个Controller类的顶部,表示该Controller下的
二、带参数的自定义Filter
首先,还是按照之前添加自定义过滤器的方法,添加一个自定义过滤器,只是里面多了一个属性,代码如下:
public class FilterAttribute : ActionFilterAttribute
{
public string Message { get; set; } public override void OnActionExecuting(ActionExecutingContext filterContext)
{
base.OnActionExecuting(filterContext);
filterContext.HttpContext.Response.Write("Action执行之前" + Message + "<br />");
} public override void OnActionExecuted(ActionExecutedContext filterContext)
{
base.OnActionExecuted(filterContext);
filterContext.HttpContext.Response.Write("Action执行之后" + Message + "<br />");
} public override void OnResultExecuting(ResultExecutingContext filterContext)
{
base.OnResultExecuting(filterContext);
filterContext.HttpContext.Response.Write("返回Result之前" + Message + "<br />");
} public override void OnResultExecuted(ResultExecutedContext filterContext)
{
base.OnResultExecuted(filterContext);
filterContext.HttpContext.Response.Write("返回Result之后" + Message + "<br />");
}
}
后在调用过滤器的时候,添加上该参数,Controller代码如下:
[Filter(Message="刘备")] //参数给上
public ActionResult Index()
{
return View();
}
如果标签打到Controller上的话,TestFilterAttributeFilter将作用到Controller下的所有的Action。
默认情况下Action上打了某个自定义标签后,虽然在Controller上也打上了此标签,但它只有Action上的标签起作用了。
补充:如果Action没有打上该标签,那么Controller上的标签便会被执行。
如果想让Action上的标签执行一次,然后Controller上的标签也执行一次,那么应该如何操作呢?
我们只需在FilterAttribute类的定义上打上标记[AttributeUsage(AttributeTargets.All, AllowMultiple = true)]即可【下面类的最上面红色字体部分】,也就是让其成为可以多次执行的Action。代码如下:
[AttributeUsage(AttributeTargets.All,AllowMultiple = true)]
public class FilterAttribute : ActionFilterAttribute
{
public string Message { get; set; }
......
三、全局过滤器
public static void RegisterGlobalFilters(GlobalFilterCollection filters)
{
filters.Add(new HandleErrorAttribute());
//注册全局过滤器
filters.Add(new TestFilterAttribute() { Message="全局"});
}
这样就每个Action都会执行此过滤器,而不必每个Controller顶部都加上标签。
MVC之 自定义过滤器(ActionFilterAttribute)的更多相关文章
- MVC之自定义过滤器(ActionFilterAttribute)
一.自定义Filter 自定义Filter需要继承ActionFilterAttribute抽象类,重写其中需要的方法,来看下ActionFilterAttribute类的方法签名. //表示所有操作 ...
- MVC之 自定义过滤器(Filter)
MVC之 自定义过滤器(Filter) 一.自定义Filter 自定义Filter需要继承ActionFilterAttribute抽象类,重写其中需要的方法,来看下ActionFilterAttri ...
- asp.net MVC之 自定义过滤器(Filter) - shuaixf
一.系统过滤器使用说明 1.OutputCache过滤器 OutputCache过滤器用于缓存你查询结果,这样可以提高用户体验,也可以减少查询次数.它有以下属性: Duration :缓存的时间, 以 ...
- asp.net MVC之 自定义过滤器(Filter)
一.系统过滤器使用说明 1.OutputCache过滤器 OutputCache过滤器用于缓存你查询结果,这样可以提高用户体验,也可以减少查询次数.它有以下属性: Duration:缓存的时间,以秒为 ...
- [转]MVC之 自定义过滤器(Filter)
本文转自:http://www.cnblogs.com/kissdodog/archive/2013/01/21/2869298.html 一.自定义Filter 自定义Filter需要继承Actio ...
- 利用MVC的自定义过滤器FilterAttribute、IActionFilter、IExceptionFilter实现异常处理等功能
今天在博客园上看了一篇推荐文章,还说得蛮有道理: http://www.cnblogs.com/richieyang/p/4779028.html 项目中确实有各种后台验证过程,最常见的莫过于判空,而 ...
- MVC 自定义过滤器/特性来实现登录授权及验证
最近悟出来一个道理,在这儿分享给大家:学历代表你的过去,能力代表你的现在,学习代表你的将来. 十年河东十年河西,莫欺少年穷 学无止境,精益求精 最近在做自学MVC,遇到的问题很多,索性一点点总结 ...
- MVC系统过滤器、自定义过滤器
一.系统过滤器使用说明 1.OutputCache过滤器 OutputCache过滤器用于缓存你查询结果,这样可以提高用户体验,也可以减少查询次数.它有以下属性: Duration:缓存的时间,以秒为 ...
- ASP.NET MVC 系统过滤器、自定义过滤器
一.系统过滤器使用说明 1.OutputCache过滤器 OutputCache过滤器用于缓存你查询结果,这样可以提高用户体验,也可以减少查询次数.它有以下属性: Duration:缓存的时间,以秒为 ...
随机推荐
- Spark开发第一个程序
simon@simon-Lenovo-G400:~/.ssh$ touch authorized_keyssimon@simon-Lenovo-G400:~/.ssh$ cat id_rsa.pub ...
- python爬虫之git的使用
一.简单认识: 1.初始化文件夹为版本控制文件夹,首先建立一个文件夹,进入这个文件夹以后输入git init初始化这个文件夹. 2.Git几种位置概念 1.本地代码:本地更改完代码以后,虽然是存放在g ...
- Protocol buffers--python 实践(二) protocol buffers vs json
为什么专门开一个坑,来使用pb.放弃本在各平台上都支持得很好的json而使用pb的一个归根到底的理由,就是希望在保证强类型和跨平台的情况下,能够更轻,更快,更简单.既然是奔着这个目标去的,到底多快我需 ...
- MyISAM索引和InnoDB索引的区别
首先你要知道: 无论是Myisam和Innodb引擎,如果在建表的时候没有显示的定义一行主键列的话,他内部都会自动创建一个隐藏的主键索引: 主键索引以外的索引假设称为次索引:首先Myisam和Inno ...
- How to ssh
ssh -p 22 cuthead@127.0.0.1
- HJ212 CRC 16 (C#)
算法 CRC16 校验寄存器赋值为 0xFFFF: 取被校验串的第一个字节赋值给临时寄存器: 临时寄存器与 CRC16 校验寄存器的高位字节进行"异或"运算,赋值给 CRC16 校 ...
- 使用update可以防止并发问题(保证数据的准确性),如果使用select会产生并发问题 ; select * from xx for update 给查询开启事务,默认情况下是没有事物的
update可以锁住数据防止数据被更新且导致与查询出的数据有误差,如果响应条数为0.说明更新失败 则可以回滚事务;
- Nginx 防盗链 secure_link 模块
L:76 需要通过 --with-http_secure_link_module 编译进Nginx secure_link 指令 Syntax: secure_link expression; Def ...
- Nginx+Tomcat 负载均衡集群
案例分析 通常情况下,一台Tomcat站点由于可能出现单点故障及无法应对多客户复杂多样性的请求等问题,不能单独应用于生产环境下,所以我们需要一套更可靠的解决方案来完善Web站点架构. Nginx是一款 ...
- kubernetes 利用label标签来绑定到特定node运行pod
利用label标签来绑定到特定node运行pod: 不如将有大量I/O的pod部署到配置了ssd的node上或者需要使用GPU的pod部署到某些安装了GPU的节点上 查看节点的标签: kubectl ...