MVC过滤器
MVC框架支持四种不同类型的过滤器,每一种类型允许你再请求处理管道的不同点上引入逻辑。
这四种过滤器类型描述于下表:
MVC框架的过滤器类型
过滤器类型 |
接口 |
默认实现 |
描述 |
Authorization |
I AuthorizationFilter |
AuthorizeAttribute |
最先运行,在任何其他过滤器和动作方法之前 |
Action |
IActionFilter |
ActionFilterAttribute |
在动作方法之前及之后运行 |
Result |
IResultFilter |
ActionFilterAttribute |
在动作结果被执行之前和之后运行 |
Exception |
IExceptionFilter |
HandlerErrorAttribute |
仅在另一个过滤器、动作方法或动作结果抛出异常时运行 |
在MVC调用一个动作之前,它首先检测该方法的定义,以查看它是否具有实现上面表中所列接口的注解属性,如果有,那么在请求管道的相应点上调用这些接口所定义的方法。
过滤器既可被应用于动作方法,也可以运用于整个控制器。
(自己理解:过滤器的作用相当于ASP.NET中的基类,如BasePage,用于处理共同的逻辑)
如果为控制器定义了一个自定义基类,那么,运用于这个基类上的任何过滤器都会影响其派生类。
1、使用授权过滤器
授权过滤器是首先运行的过滤器,它运行在其他过滤器之前以及动作方法被调用之前。
创建授权过滤器最简单的办法是创建AuthorizeAttribute类的子类,并重写其AuthorizeCore方法,这确保开发者能够利用内建的AuthorizeAttribute。
如:

应用:
2、使用异常过滤器
这种异常来自:
◆另一个过滤器(授权、动作、结果过滤器)
◆动作方法本身
◆当动作结果被执行时
(备注:异常处理器可用于自定义错误处理)
3、使用动作和结果过滤器
动作和结果过滤器是可以被用于任何目的的多用途过滤器。
OnActionExecuting
下面是具体项目中使用动作过滤器的示例(不太规范):
using SFast; using System; using System.Collections.Generic; using System.Configuration; using System.Linq; using System.Web; using System.Web.Mvc; using System.Web.Script.Serialization; using XFK.JDTrain.Common; using XinfuMall.XinfuWeb.PublicClass; namespace XFK.JDTrain.Web.Controllers { public class BaseController : Controller { // // GET: /Base/ public JavaScriptSerializer js = new JavaScriptSerializer(); public LogHelper log = null ; public UserSession user { get ; set ; } public EnterpriseAccountSession enterprise { get ; set ; } /// <summary> /// 执行控制器中方法之前执行的方法 /// </summary> /// <param name="filterContext"></param> protected override void OnActionExecuting(ActionExecutingContext filterContext) { LogHelper.WriteLog( "进入BaseController" ); TempData[ "xinfuUrl" ] = System.Configuration.ConfigurationManager.AppSettings[ "xinfuUrl" ]; if (XFK.Infrastructure.Util.SessionManager.Read(UserUtil.user_sessionStr) != null ) { user = XFK.Infrastructure.Util.SessionManager.Read(UserUtil.user_sessionStr) as UserSession; object enterpriseId = XFK.Infrastructure.Util.SessionManager.Read(EnterpriseUtil.enterprise_sessionId); enterprise = new EnterpriseAccountSession(); enterprise.Id = Convert.ToInt32(enterpriseId); ViewBag.UserName = user.AccountName; ViewBag.Enterpriseid = Convert.ToInt32(enterpriseId); var limitEnterprise = String.IsNullOrEmpty(ConfigurationManager.AppSettings[ "limitEnterprise" ]) ? "" : ConfigurationManager.AppSettings[ "limitEnterprise" ]; if (limitEnterprise.Split( ',' ).Contains(enterprise.Id.ToString())) { System.Web.HttpContext.Current.Response._WriteEnd( "对不起,所属的企业没有订购此产品的权限!" ); } } else { filterContext.Result = new RedirectResult(ConfigurationManager.AppSettings[ "xinfuUrl" ] + "mall/Login?originUrl=" + MyUrl.UrlEncode(Request.Url.ToString())); LogHelper.WriteLog( "当前访问的路径是:" + ConfigurationManager.AppSettings[ "xinfuUrl" ] + "mall/Login?originUrl=" + MyUrl.UrlEncode(Request.Url.ToString())); } base .OnActionExecuting(filterContext); } } } |
本文内容参考:《精通ASP.NET MVC3框架》第13章
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 零经验选手,Compose 一天开发一款小游戏!
· 通过 API 将Deepseek响应流式内容输出到前端