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: ...
随机推荐
- Asp.net 用 Graphics 统计图(柱状图, 折线图, 扇形图)
统计图形种类繁多, 有柱状图, 折线图, 扇形图等等, 而统计图形的绘制方法也有很多, 有Flash制作的统计图形, 有水晶报表生成统计图形, 有专门制图软件制作, 也有编程语言自己制作的:这里我们用 ...
- C++多态的实现与局限性
1.什么是多态? 父类指针指向子类对象,运行时期调用方法的时候,根据方法拥有者的真实类型,确定调用哪个方法. 2.如何实现多态? 要实现多态,需要加一个中间层,暴露父类的方法,内部根据指针的真实类型决 ...
- C#-datagridview右键选中行
在datagridview中有时需要在右键点击某行的时候就选中它,那么我们只需要在datagridview的CellMonseDown事件中添加如下代码就行: && e.ColumnI ...
- "无法启动程序,因为计算机中丢失*.dll” 运行exe错误解决方法
笔者把编译生成的win32 Release exe文件复制到另外一台电脑上,却发现程序不能运行,报错如下: 报错提示缺失动态链接库pcl_common_release.dll,那为什么在编译生成的电脑 ...
- External file changes sync may be slow: Project files cannot be watched (are they under network mount?)
if some files are on a mounted disk: go to Settings | Notifications | File Watcher Messages and tune ...
- Struts2中的类型转换
1. Struts2中的类型转换 我们知道通过HTTP提交到后台的数据,都是字符串的形式,而我们需要的数据类型当然不只字符串类型一种.所以,我们需要类型转换! 在Struts2中,类型转换的概 ...
- slave_net_timeout
http://blog.csdn.net/lwei_998/article/details/46864453
- 解决安装包在win7,win8系统下安装后运行没有管理员权限
今天打包一个程序在客户机上安装运行:一直报没有管理员权限:客户机是win8系统:直接右键管理员身份运行则都可以:为了避免不让用户每次都这么麻烦:只有问哈群友和百度,终于找到解决方法: 第一步:项目属性 ...
- 如何把 excel 设为文本格式?
选择要设置的单元格,右键选择 --- “设置单元格格式” --- 选 “ 分类 ” 下面的 “ 文本 ” --- 确定. 修改前: 修改后:
- C# 之 获取文件名及拓展名
1.用Path类的方法(最常用) string fullPath = @"\WebSite\Default.aspx"; string filename = System.IO.P ...