Action Filters for ASP.NET MVC
本文主要介绍ASP.NET MVC中的Action Filters,并通过举例来呈现其实际应用。
Action Filters 可以作为一个应用,作用到controller action (或整个controller action中),以改变action的行为。
ASP.NET MVC Framework支持四种不同类型的Filter:
- Authorization filters – 实现
IAuthorizationFilter接口的属性. - Action filters – 实现
IActionFilter接口的属性. - Result filters – 实现
IResultFilter接口的属性. - Exception filters – 实现
IExceptionFilter接口的属性。
Filter 的默认执行顺序,如上面的编号顺序。验证(authorization)filter永远都是最开始执行的,异常(exception)filter永远都是最后执行的。
下面只详细介绍Action Filters。
在Action Filters中 ASP.NET MVC Framework 提供了ActionFilterAttribute父类。

所以在Action,Result执行之前之后分别做一些操作,就需要重写ActionFilterAttribute父类,并实现父类中的虚方法。
下面,我们假设一种场景。当我们打开管理系统的页面时,需要进行权限检查,假如多个页面都需要权限检查,重复的代码,必然会让我们感到抓狂。
这时候就可以利用ActionFilterAttribute父类重写OnActionExecuting()方法进行权限检查。
首先,我们在App_Start中添加一个新类,继承ActionFilterAttribute父类。代码如下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc; namespace MvcMobileDMS.App_Start
{
//[AttributeUsage(AttributeTargets.All, AllowMultiple = true)]
public class ActionAttributeFilter : ActionFilterAttribute
{
public override void OnActionExecuting(ActionExecutingContext filterContext)
{ //在Action执行前执行
if (filterContext.HttpContext.Session["temp"] == null)
{
filterContext.HttpContext.Response.Redirect("~/dms/logon");
}
//filterContext.HttpContext.Response.Redirect("dms/add"); base.OnActionExecuting(filterContext);
}
}
}
我们假设当系统中存在Session["temp"],则验证通过。否则跳到登陆页面。
好了,ActionFilterAttribute父类已经重写,那么如何运用到我们的Action中呢?很简单,看代码:
[MvcMobileDMS.App_Start.ActionAttributeFilter()]
public ActionResult Add()
{
Session.Remove("temp");
return View();
}
在Action 方法上添加[MvcMobileDMS.App_Start.ActionAttributeFilter()]即可。即我们新增的类。这样每次访问add页面就要做权限检查。
那么,我们再想深一层,假如我想所有的页面都执行权限检查呢?看代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc; namespace MvcMobileDMS.Controllers
{
[MvcMobileDMS.App_Start.ActionAttributeFilter()]
public class DMSController : Controller
{
//
// GET: /DMS/
//[MvcMobileDMS.App_Start.ActionAttributeFilter()]
public ActionResult Index()
{ //AVON.DMS.DAL.Entities DMS = new AVON.DMS.DAL.Entities();
//AVON.DMS.Model.REP A = DMS.REP.SingleOrDefault<AVON.DMS.Model.REP>(t => t.NO == "00561874");
//AVON.DMS.BLL.Rep repBLL = new AVON.DMS.BLL.Rep();
//AVON.DMS.Model.REP A = repBLL.GetFromRep("00561874");
//ViewData["mydata"] = A.JOINDATE;
ViewData["mydata"] = "index";
return View();
} //[MvcMobileDMS.App_Start.ActionAttributeFilter()]
public ActionResult Add()
{
Session.Remove("temp");
return View();
} public ActionResult logon()
{
Session["temp"] = "aaa";
return View();
} public string test()
{
return "hahah";
}
}
}
在DMSController类前,我添加了[MvcMobileDMS.App_Start.ActionAttributeFilter()],如此,所有的action在运行前都会执行OnActionExecuting方法,即权限检查。
如果,我们再更想深一层,我们想所有的Controller都执行呢?也很简单,只需修改Global.asax代码,如下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Http;
using System.Web.Mvc;
using System.Web.Optimization;
using System.Web.Routing; namespace MvcMobileDMS
{
// Note: For instructions on enabling IIS6 or IIS7 classic mode,
// visit http://go.microsoft.com/?LinkId=9394801 public class MvcApplication : System.Web.HttpApplication
{
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
GlobalFilters.Filters.Add(new MvcMobileDMS.App_Start.ActionAttributeFilter());
WebApiConfig.Register(GlobalConfiguration.Configuration);
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
RouteConfig.RegisterRoutes(RouteTable.Routes);
BundleConfig.RegisterBundles(BundleTable.Bundles);
}
}
}
只要添加全局GlobalFilter即可。GlobalFilters.Filters.Add(new MvcMobileDMS.App_Start.ActionAttributeFilter());
希望对你有所帮助O(∩_∩)O哈哈~。
Action Filters for ASP.NET MVC的更多相关文章
- 8. Filters in ASP.NET MVC 5.0【ASP.NET MVC 5.0中的过滤器】
ASP.NET Filers用来在MVC框架的不同请求处理阶段,注入额外的逻辑.过滤器为横切关注点提供了一种方法(日志记录,授权,缓存). 在这篇文章中,我将会向你介绍MVC框架支持的各种不同种类过滤 ...
- 获取action name在asp.net mvc
Update for MVC 3 ViewContext.Controller.ValueProvider.GetValue("action").RawValue ViewCont ...
- Asp.net MVC十问十答[译]
1. Explain MVC (Model-View-Controller) in general? MVC (Model-View-Controller) is an architectural s ...
- 理解ASP.NET MVC Framework Action Filters
原文:http://www.cnblogs.com/darkdawn/archive/2009/03/13/1410477.html 本指南主要解释action filters,action filt ...
- HTTP Modules versus ASP.NET MVC Action Filters
from:http://odetocode.com/blogs/scott/archive/2011/01/17/http-modules-versus-asp-net-mvc-action-filt ...
- ASP.NET MVC的Action Filter
一年前写了一篇短文ASP.NET MVC Action Filters,整理了Action Filter方面的资源,本篇文章详细的描述Action Filter.Action Filter作为一个可以 ...
- ASP.NET MVC 4.0的Action Filter
有时候你想在调用action方法之前或者action方法之后处理一些逻辑,为了支持这个,ASP.NET MVC允许你自定义创建action过滤器.Action过滤器是自定义的Attributes,用来 ...
- ASP.NET MVC和ASP.NET Core MVC中获取当前URL/Controller/Action (转载)
ASP.NET MVC 一.获取URL(ASP.NET通用): [1]获取完整url(协议名+域名+虚拟目录名+文件名+参数) string url=Request.Url.ToString(); [ ...
- Asp.net mvc 知多少(八)
本系列主要翻译自<ASP.NET MVC Interview Questions and Answers >- By Shailendra Chauhan,想看英文原版的可访问[http: ...
随机推荐
- SHH入门:Spring框架简介
(1)Spring 七大模块 核心容器:核心容器提供Spring 框架的基本功能.核心容器的主要组件是 BeanFactory,它是工厂模式的实现.BeanFactory 使用控制反转 (IOC) 模 ...
- zxing 生成二维码
一.zxing介绍 zxing是google提供生成.解析一维码.二维码的开源库. 二.使用 2.1 maven pom 配置 <dependency> <groupId>co ...
- 最牛X的编码套路
最近,我大量阅读了Steve Yegge的文章.其中有一篇叫"Practicing Programming"(练习编程),写成于2005年,读后令我惊讶不已: 与你所相信的恰恰相反 ...
- centos6下安装部署hadoop2.2
环境准备1.操作系统:centos6.0 64位2.hadoop版本:hahadoop-2.2.0 安装和配置步骤具体如下:1.主机和ip分配如下 ip地址 ...
- 教你50招提升ASP.NET性能(六):为了生动的用户体验,总是在客户端验证
(12)For a snappy user experience, always validate on the client 招数12: 为了生动的用户体验,总是在客户端验证 To avoid un ...
- 找回丢失的SQL Server性能计数器
There was one time when I was delivering a Service using a tool that gathers performance data throug ...
- 关于写blog这件事
事实上一直是挺喜欢写blog的.可是近期在写blog这件事上遇到或者開始思考一些问题了. 首先,写blog的动机.对于这个问题,我从自己的理解上得出下面几个原因: 写blog是对自己学到知识的一种总 ...
- Java路径操作具体解释
1.基本概念的理解 绝对路径:绝对路径就是你的主页上的文件或文件夹在硬盘上真正的路径.(URL和物理路径)比如: C:\xyz\test.txt 代表了test.txt文件的绝对路径.http://w ...
- android学习日记15--WebView(网络视图)
一.WebView 1.简述 WebView(网络视图)内置WebKit引擎,能加载显示网页,还支持JS,并且能够在Android平台使用AJAXWebView可以在布局中声明,也可以在Activit ...
- 应聘.net开发工程师常见的面试题(五)
1.描述一下C#中索引器的实现过程,是否只能根据数字进行索引? 答:不是.可以用任意类型. 2.在C#中,string str = null 与 string str = ” ” 请尽量使用文字或图象 ...