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 ...
 
随机推荐
- vmware   vmware esxi5.x安装方法及解决错误
			
vmware Esxi5.x安装指南 vmware Esxi5文档中心官网地址: http://pubs.vmware.com/vsphere-55/index.jsp?topic=%2Fcom.vm ...
 - bootstrap学习(四)输入框、导航
			
输入框组: 基本用法: //form-control 占满 //input-group:输入框组//input-group-addon:输入框前加入一个前缀 <div class="i ...
 - 转载JAVA八大经典书籍,你看过几本?
			
一.Java从入门到精通*<Java从入门到精通(第3版)>从初学者角度出发,通过通俗易懂的语言.丰富多彩的实例,详细介绍了使用Java语言进行程序开发需要掌握的知识.<Java从入 ...
 - 6、OpenCV Python 图像模糊
			
__author__ = "WSX" import cv2 as cv import numpy as np #均值模糊 中值模糊 自定义模糊(卷积) #卷积原理 #均值模糊 de ...
 - 远程私有库的创建 pod  组件化
			
参考: http://www.cnblogs.com/hs-funky/p/6780203.html http://www.jianshu.com/p/4b63dfbd8be7 http://ww ...
 - thinkphp 3.0 核心函数U的一个致命bug
			
最近在玩thinkphp,感觉内置函数 U 挺强大的! 传递多个参数时,出乱子了(window环境下,xampp) 例如 echo U('Blog/cate',array('cate_id'=> ...
 - 001 开发环境搭建、安卓项目结构、R文件位置、asset目录创建
			
1.安卓开发平台搭建 (1)下载SDK基础工具包(自己的百度云中) (2)将下载的安装包(android-sdk_r24.4.1-windows.zip)解压后,放到以下路径 C:\SoftAppli ...
 - POJ 1000 A+B
			
#include <stdio.h> int main() { int a,b; scanf("%d %d",&a, &b); printf(" ...
 - Experimental Educational Round: VolBIT Formulas Blitz C
			
Description The numbers of all offices in the new building of the Tax Office of IT City will have lu ...
 - Linux安装Sqlmap等工具
			
简单记录一下安装过程,都是小白教程,省的哪天又忘了要去百度. 1.下载sqlmap 源码进行安装 wget https://github.com/sqlmapproject/sqlmap/tarbal ...