MVC4 过滤器使用和怎样控制全部action和部分action
MVC中的过滤器分四种分别为:IActionFilter(动作过滤器), IAuthorizationFilter(授权过滤器), IExceptionFilter(异常过滤器), IResultFilter(结果过滤器)字面翻译,凑合理解吧。
在此就那IActionFilter举例,在这个接口中有两个方法,分别是:OnActionExecuting(Action执行前执行)和OnActionExecuted(Action执行后执行),

现在我们要想让一个Controller中的所有Action都执行这个过滤器就需要对里面的方法进行重写
public class LoginController : Controller
{
//
// GET: /Login/
public ActionResult Index()
{
return View();
} public ActionResult Login()
{
string name = HttpContext.Request["UserName"];
ViewData["name"] = name;
return View();
}
protected override void OnActionExecuting(ActionExecutingContext filterContext)
{
//object[] attrs = filterContext.ActionDescriptor.GetCustomAttributes(typeof(NoFilter), true);
//if (attrs.Length == 1)//有NoFilter属性
//{
// return;
//} string name = filterContext.HttpContext.Request["UserName"];
if (string.IsNullOrEmpty(name))
{
filterContext.HttpContext.Response.Write("<script>alert('名称不能为空!');</script>");
filterContext.HttpContext.Response.End();
}
}
}
这样每个action在执行前都会先执行这个过滤器。
下面是怎样让Index的Action不执行,只是对Login执行。有2种方式实现:
第一种:代码修改如下:
public class LoginController : Controller
{
//
// GET: /Login/
[NoFilter]
public ActionResult Index()
{
return View();
} public ActionResult Login()
{
string name = HttpContext.Request["UserName"];
ViewData["name"] = name;
return View();
}
protected override void OnActionExecuting(ActionExecutingContext filterContext)
{
//过滤掉标有NoFilter标签的Action
object[] attrs = filterContext.ActionDescriptor.GetCustomAttributes(typeof(NoFilter), true);
if (attrs.Length == )//有NoFilter属性
{
return;
} string name = filterContext.HttpContext.Request["UserName"];
if (string.IsNullOrEmpty(name))
{
filterContext.HttpContext.Response.Write("<script>alert('名称不能为空!');</script>");
filterContext.HttpContext.Response.End();
}
}
}
public class NoFilter : FilterAttribute, IActionFilter
{
public void OnActionExecuting(ActionExecutingContext filterContext)
{
//
}
public void OnActionExecuted(ActionExecutedContext filterContext)
{
//
}
}
第二种:代码修改如下:
public class LoginController : Controller
{
//
// GET: /Login/
public ActionResult Index()
{
return View();
}
[LoginFilter]
public ActionResult Login()
{
string name = HttpContext.Request["UserName"];
ViewData["name"] = name;
return View();
}
//protected override void OnActionExecuting(ActionExecutingContext filterContext)
//{
// //过滤掉标有NoFilter标签的Action
// object[] attrs = filterContext.ActionDescriptor.GetCustomAttributes(typeof(NoFilter), true);
// if (attrs.Length == 1)//有NoFilter属性
// {
// return;
// } // string name = filterContext.HttpContext.Request["UserName"];
// if (string.IsNullOrEmpty(name))
// {
// filterContext.HttpContext.Response.Write("<script>alert('名称不能为空!');</script>");
// filterContext.HttpContext.Response.End();
// }
//}
}
public class LoginFilter : FilterAttribute, IActionFilter
{
public void OnActionExecuting(ActionExecutingContext filterContext)
{ string name = filterContext.HttpContext.Request["UserName"];
if (string.IsNullOrEmpty(name))
{
filterContext.HttpContext.Response.Write("<script>alert('名称不能为空!');</script>");
filterContext.HttpContext.Response.End();
}
}
public void OnActionExecuted(ActionExecutedContext filterContext)
{
//
}
}
补充:1.自定义过滤器Filter必须继承FilterAttribute。
2.定义过个过滤器可以定义过滤器执行的先后顺序 例如: 在Action上标注:
[NoFilter(Order=2)]
[LoginFilter(Order=1)]
这样当执行这个Action时候会先执行LoginFilter 再执行NoFilter。
MVC4 过滤器使用和怎样控制全部action和部分action的更多相关文章
- MVC4过滤器(转)
先来看看一个例子演示过滤器有什么用: public class AdminController : Controller { // ... instance variables and constru ...
- MVC4 过滤器(转)
先来看看一个例子演示过滤器有什么用: public class AdminController : Controller { // ... instance variables and constru ...
- asp.net mvc4 过滤器的简单应用:登录验证
直接上代码,不要说话. ASP.NET MVC4过滤器的简单应用:验证登录 [AcceptVerbs(HttpVerbs.Post)] public ActionResult login(FormCo ...
- Struts2基础-2 -实现Action接口创建Action控制器
1.新建一个web项目,目录结构如下,添加jar包到lib文件夹里,并把jar包add 到 buildpath里面 2.web.xml配置 struts2的过滤器类:StrutsPrepareAndE ...
- struct2的structs.xml文件配置There is no Action mapped for action name 问题
很久没写过博客,今天重新开始写,新技术太多,只有通过博客才可以不断积累,本人水平有限,如有错误,欢迎指正,谢谢 今天在MAVEN上配置web project的struct2,发现自己忽略了很多问题,再 ...
- [Microsoft Dynamics CRM 2016]Invalid Action – The selected action was not valid 错误的诱因及解决方法
详细问题描述: 由于解决windows server 评估版过期\SQL server 评估版过期的问题后而导致的Invalid Action – The selected action was no ...
- There is no Action mapped for namespace [/pages/action/student] and action name [findStudent]
1.错误描写叙述 2014-7-13 2:38:54 org.apache.jasper.compiler.TldLocationsCache tldScanJar 信息: At least one ...
- 实现Action(含Action访问ServletAPI)
Action里是否包含实例变量不重要,重要的是包含setter和getter方法. Action可用于封装请求参数和处理结果.jsp中使用struts2输出:<s:property value= ...
- Android官方导航栏ActionBar(二)—— Action View、Action Provider、Navigation Tabs的详细用法
在上一篇文章(Android之官方导航栏ActionBar)中,我们介绍了ActionBar各组成部分的基本应用.ActionBar除了提供Action Buttons外,还提供了多种导航方式如 Ac ...
随机推荐
- 为所有的Ul下的li标签添加点击事件
- git 本地分支与远程分支相关操作记录
1.远程分支中有新增分支,但自己的本地分支没有对应同步 git checkout -b [remote-branch-name] origin/[remote-branch-name] 2. 查看本地 ...
- [SCOI2010]序列操作 BZOJ1858 线段树
题目描述 lxhgww最近收到了一个01序列,序列里面包含了n个数,这些数要么是0,要么是1,现在对于这个序列有五种变换操作和询问操作: 0 a b 把[a, b]区间内的所有数全变成0 1 a b ...
- 合并queryset
今天在实现搜索时遇到一个问题,如何同时搜索model里面的title以及content和category字典 contents = Blog.objects.filter(content__conta ...
- shell脚本学习一
shell脚本是一种程序与linux内核的语言: 第一个shell脚本: #!/bin/bash echo "cxy" 就是输出cxy 如何执行这个脚本呢: cd demo 进入s ...
- Unity组件
在学习C++的时候,对于面对对象有点了解.然后也使用过一段时间的Unity,用起来还是觉得,怎么这么好用.耦合性极低.当时不知道这是基于组件编程.所以现在来学习下基于组件的知识,并比较下基于组件和基于 ...
- Codeforces Round #335 (Div. 2) B
B. Testing Robots time limit per test 2 seconds memory limit per test 256 megabytes input standard i ...
- drozer与adb工具的安装与使用
drozer:链接: https://pan.baidu.com/s/1skTJdgh 密码: wah1 adb:链接: https://pan.baidu.com/s/1gfpIkuv 密码: n8 ...
- Tomcat-猫
第1章 Tomcat简介 Tomcat 是一个web服务器 ,类似nginx,apache的http Nginx http 只能处理html等静态文件jpg() 网页分为静态网页(以.html 或 ...
- js遍历table和gridview
//遍历table var tableObj = document.getElementById("tableName");var str = "";for(v ...