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. UIViewController+MJPopupViewController

    1.MJPopupBackgroundView 1.1 MJPopupBackgroundView.h // // MJPopupBackgroundView.h // watched // // C ...

  2. Tomcat—Bad Request

    前段时间,由于搭建环境的问题,项目暂停了一个多月,终于再次拿起来了,可是历史问题还是没有解决,再次让问题重现了一把. 上面的图片的大概意思就是:错误请求(无效主机名称)     看到这个,我一开始是兴 ...

  3. Java——事务

    一.事务(Transaction) 1. 在开发中我们的一个业务往往需要同时操作多个表,这些操作往往是不可分割,业务中的对数据库的多次操作,要么同时成功,要么全都失败. 2.注意:我们在同一个事务中使 ...

  4. memcache (持续了解ing...)

    mem cache 英[kæʃ] 美[kæʃ]vt. 贮藏; memcache是一套分布式的高速缓存系统,目前被许多网站使用以提升网站的访问速度,尤其对于一些大型的.需要频繁访问数据库的网站访问速度提 ...

  5. 模板 Trie树

    模板 Trie树 code: #include <iostream> #include <cstdio> using namespace std; const int wx=2 ...

  6. linux下虚拟主机配置

    <VirtualHost *:80>ServerAdmin  admin@localhostServerName   www.baidu.org DocumentRoot "/d ...

  7. 再谈hive-1.0.0与hive-1.2.1到JDBC编程忽略细节问题

    不多说,直接上干货,这个问题一直迷惑已久,今天得到亲身醒悟. 所以,建议hadoop-2.6.0.tar.gz的用户与hive-1.0.0搭配使用.当然,也可以去用高版本去覆盖它. log4j:WAR ...

  8. spring-redis 存储数据

    package com.fndsoft.bcis.utils; import org.springframework.beans.factory.annotation.Autowired;import ...

  9. doors dxl 遍历object 查找

    Module m = current; //m = edit(“xxx”) Object o for o in m do { string sht = o.”shtName” Buffer bf=cr ...

  10. 学习C/C++需要掌握哪些知识

    初级阶段 1.C语言 数据类型.变量.内存布局.指针基础: 字符串.一维数组.二维数组: 一级指针,二级指针,三级指针,N级指针概念,指针数组和数组指针: 结构体.文件的使用: 动态库的封装和设计: ...