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的更多相关文章

  1. MVC4过滤器(转)

    先来看看一个例子演示过滤器有什么用: public class AdminController : Controller { // ... instance variables and constru ...

  2. MVC4 过滤器(转)

    先来看看一个例子演示过滤器有什么用: public class AdminController : Controller { // ... instance variables and constru ...

  3. asp.net mvc4 过滤器的简单应用:登录验证

    直接上代码,不要说话. ASP.NET MVC4过滤器的简单应用:验证登录 [AcceptVerbs(HttpVerbs.Post)] public ActionResult login(FormCo ...

  4. Struts2基础-2 -实现Action接口创建Action控制器

    1.新建一个web项目,目录结构如下,添加jar包到lib文件夹里,并把jar包add 到 buildpath里面 2.web.xml配置 struts2的过滤器类:StrutsPrepareAndE ...

  5. struct2的structs.xml文件配置There is no Action mapped for action name 问题

    很久没写过博客,今天重新开始写,新技术太多,只有通过博客才可以不断积累,本人水平有限,如有错误,欢迎指正,谢谢 今天在MAVEN上配置web project的struct2,发现自己忽略了很多问题,再 ...

  6. [Microsoft Dynamics CRM 2016]Invalid Action – The selected action was not valid 错误的诱因及解决方法

    详细问题描述: 由于解决windows server 评估版过期\SQL server 评估版过期的问题后而导致的Invalid Action – The selected action was no ...

  7. 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 ...

  8. 实现Action(含Action访问ServletAPI)

    Action里是否包含实例变量不重要,重要的是包含setter和getter方法. Action可用于封装请求参数和处理结果.jsp中使用struts2输出:<s:property value= ...

  9. Android官方导航栏ActionBar(二)—— Action View、Action Provider、Navigation Tabs的详细用法

    在上一篇文章(Android之官方导航栏ActionBar)中,我们介绍了ActionBar各组成部分的基本应用.ActionBar除了提供Action Buttons外,还提供了多种导航方式如 Ac ...

随机推荐

  1. 实现简易Web服务器(c语言)

    任务: (1)实现服务器与客户端间的通信. (2)可以实现HTTP请求中的GET方法. (3)提供静态网页浏览功能,如可浏览:HTML页面,无格式文本,常见图像格式等. (4)提供可以传递参数的动态网 ...

  2. Nodejs 文档概览

    Node.js v8.11.1 Node.js v8.11.1 文档 今天大致浏览了一下Node.js的官方文档,走马观花的了解了大部分模块的api,对他们的使用场景做一个简单的笔记 assert 断 ...

  3. docker与虚拟机性能比较(转)

    http://blog.csdn.net/cbl709/article/details/43955687 本博客来源于我的个人博客: www.chenbiaolong.com 欢迎访问. 概要 doc ...

  4. linux文件系统相关资料

             linux下文件系统通常是通过虚拟文件系统(VFS)蔽下层具体文件系统操作的差异,为上层的操作提供一个统一的接口.文件系统底层都是用系统IO缓存层提供的块读写接口,实现逻辑块到物理块 ...

  5. win10全半角切换

    shift+sapce shift+sapce:全半角切换快捷键,编程的时候发现英文是这种状态,就需要用快捷键切换成半角. (查过老是忘记,在这里写一下记住它)

  6. [国家集训队]happiness 最小割 BZOJ 2127

    题目描述 高一一班的座位表是个n*m的矩阵,经过一个学期的相处,每个同学和前后左右相邻的同学互相成为了好朋友.这学期要分文理科了,每个同学对于选择文科与理科有着自己的喜悦值,而一对好朋友如果能同时选文 ...

  7. 设置SQL脚本大小敏感

    1.设置SQL脚本大小写不敏感 USE [master]  GO  ALTER DATABASE [DatabaseName] COLLATE Chinese_PRC_CI_AI  GO 2.设置大S ...

  8. maven 子父工程。。。

    子工程module 父工程 主要是注意路径问题..否则会报错---

  9. Android 选择本地图片的demo

    https://github.com/lipanquan/SelectLocalPhoto

  10. B树与B+

    简单剖析B树(B-Tree)与B+树https://blog.csdn.net/z_ryan/article/details/79685072 B树和B+树的插入.删除图文详解https://www. ...