ASP.NET MVC学习系列(4)——MVC过滤器FilterAttribute
1、概括
MVC提供的几种过滤器其实也是一种特性(Attribute),MVC支持的过滤器类型有四种,分别是:AuthorizationFilter(授权),ActionFilter(行为),ResultFilter(结果)和ExceptionFilter(异常),他们分别对应了四个筛选器接口IAuthorizationFilter、IActionFilter、IResultFilter和IExceptionFilter。这四种筛选器都有派生于一个公共的类FilterAttribute,该类指定了筛选器的执行顺序Order和是否允许多个应用AllowedMultiple。这四种筛选器默认的执行顺序为最先进行授权筛选,最后进行异常处理,中间则是ActionFilter和ResultedFilter。官网对FilterAttribute层次结构的介绍图如下
2、MVC四大过滤器介绍
2.1、AuthorizeFilter筛选器
AuthorizeAttribute为最终授权过滤器的实现者,它实现了IAuthorizationFilter接口和FilterAttribute抽象类,接口中的OnAuthorization(AuthorizationContext filterContext)方法是最终验证授权的逻辑(其中AuthorizationContext是继承了ControllerContext类),AuthorizeCore方法是最终OnAuthorization()方法调用的最终逻辑。
- bool AuthorizeCore(HttpContextBase httpContext):授权验证的逻辑处理,返回true则是通过授权,返回false则不是。若验证不通过时,OnAuthorization方法内部会调用HandleUnauthorizedRequest
- void HandleUnauthorizedRequest(AuthorizationContext filterContext):这个方法是处理授权失败的事情。
AuthorizeCore代码如下:
protected virtual bool AuthorizeCore(HttpContextBase httpContext)
{
if (httpContext == null)
{
throw new ArgumentNullException("httpContext");
} IPrincipal user = httpContext.User;
if (!user.Identity.IsAuthenticated)
{
return false;
} if (_usersSplit.Length > && !_usersSplit.Contains(user.Identity.Name, StringComparer.OrdinalIgnoreCase))
{
return false;
} if (_rolesSplit.Length > && !_rolesSplit.Any(user.IsInRole))
{
return false;
} return true;
}
我们不一定要用MVC默认的Authorize授权验证规则,规则可以自己来定,自定义授权过滤器继承IAuthorizeAttribute和FilterAttribute,由于OnAthurization()、AuthorizeCore()和HandleUnauthorizedRequest()方法都是虚方法,这些方法是可以重写的,这样就可以自定义自己的验证规则和验证失败时的处理逻辑了。示例代码如下
public class PermissionFilterAttribute: AuthorizationFilter
{ protected override bool AuthorizeCore(HttpContextBase httpContext)
{
return true;
} protected override void HandleUnauthorizedRequest(AuthorizationContext filterContext)
{
filterContext.Result=""; }
}
授权过滤器的使用方式一如下:
[PermissionFilterAttribute]
public ActionResult index()
{
return View();
}
2.2、ActionFilter过滤器
ActionFilter过滤器是在Action方法执行前后会触发,主要用于在Action执行前后处理一些相应的逻辑。ActionFilter的过滤器都继承于ActionFilterAttribute抽象类,而它实现了IActionFilter、IResultFilter和FilterAttribute类,结构如下
因此自定义ActionFilter过滤器只要继承ActionFilterAttribute,实现其中的方法即可。我们来举一个简单的例子,获取Action方法的执行时长,代码如下
public class DefaultController : Controller
{
[ActionExecTimeSpan]
public ActionResult DoWork()
{
return View();
}
} public class ActionExecTimeSpanAttribute : ActionFilterAttribute
{
private const string executeActionTimeKey = "ActionExecBegin"; public override void OnActionExecuting(ActionExecutingContext filterContext)
{
base.OnActionExecuting(filterContext);
//记录开始执行时间
filterContext.HttpContext.Items[executeActionTimeKey] = DateTime.Now;
} public override void OnActionExecuted(ActionExecutedContext filterContext)
{
//计算执行时间,并记录日志
if (filterContext.HttpContext.Items.Contains(executeActionTimeKey))
{
DateTime endTime = DateTime.Now;
DateTime beginTime = Convert.ToDateTime(filterContext.HttpContext.Items[executeActionTimeKey]);
TimeSpan span = endTime - beginTime;
double execTimeSpan = span.TotalMilliseconds;
log.Info(execTimeSpan + "毫秒");
}
//
base.OnActionExecuted(filterContext);
}
}
2.3、ResultFilter过滤器
ResultFilter过滤器是对Action方法返回的Result结果进行执行时触发的。它也分执行前和执行后两个段执行,所有的ResultFilter都实现了IResultFilter接口和FilterAttribute类,看一下接口定义
public interface IResultFilter
{
void OnResultExecuting(ResultExecutingContext filterContext); void OnResultExecuted(ResultExecutedContext filterContext);
}
其中OnResultExecuting和OnResultExecuted方法分别是在Result执行前、后(页面展示内容生成前、后)触发。使用ResultFilter筛选器最典型的应用就是页面静态化。
2.4、ExceptionFilter过滤器(详细介绍https://www.cnblogs.com/qtiger/p/10824562.html)
该过滤器是在系统出现异常时触发,可以对抛出的异常进行处理。所有的ExceptionFilter筛选器都是实现自IExceptionFilter接口
public interface IExceptionFilter
{
void OnException(ExceptionContext filterContext);
}
实现OnException方法来实现对异常的自定义处理,MVC4中实现了默认的异常处理机制,源码如下
public virtual void OnException(ExceptionContext filterContext)
{
if (filterContext == null)
{
throw new ArgumentNullException("filterContext");
}
if (filterContext.IsChildAction)
{
return;
} // If custom errors are disabled, we need to let the normal ASP.NET exception handler
// execute so that the user can see useful debugging information.
if (filterContext.ExceptionHandled || !filterContext.HttpContext.IsCustomErrorEnabled)
{
return;
} Exception exception = filterContext.Exception; // If this is not an HTTP 500 (for example, if somebody throws an HTTP 404 from an action method),
// ignore it.
if (new HttpException(null, exception).GetHttpCode() != )
{
return;
} if (!ExceptionType.IsInstanceOfType(exception))
{
return;
} string controllerName = (string)filterContext.RouteData.Values["controller"];
string actionName = (string)filterContext.RouteData.Values["action"];
HandleErrorInfo model = new HandleErrorInfo(filterContext.Exception, controllerName, actionName);
filterContext.Result = new ViewResult
{
ViewName = View,
MasterName = Master,
ViewData = new ViewDataDictionary<HandleErrorInfo>(model),
TempData = filterContext.Controller.TempData
};
filterContext.ExceptionHandled = true;
filterContext.HttpContext.Response.Clear();
filterContext.HttpContext.Response.StatusCode = ; // Certain versions of IIS will sometimes use their own error page when
// they detect a server error. Setting this property indicates that we
// want it to try to render ASP.NET MVC's error page instead.
filterContext.HttpContext.Response.TrySkipIisCustomErrors = true;
}
Application_Start中将HandleErrorAttribute添加到全局过滤器GlobalFilterCollection中,系统即会对异常进行对应的处理。
3、其他的过滤器
3.1、OutputCache过滤器
表示一个特性,该特性用于标记将缓存其输出的操作方法。当用户访问页面时,整个页面将会被服务器保存在内存中,这样就对页面进行了缓存。当用户再次访问该页,
页面不会再次执行数据操作,页面首先会检查服务器中是否存在缓存,如果缓存存在,则直接从缓存中获取页面信息,如果页面不存在,则创建缓存。OutputCache的代码定义片段如下:
[AttributeUsageAttribute(AttributeTargets.Class|AttributeTargets.Method, Inherited = true,
AllowMultiple = false)]
public class OutputCacheAttribute : ActionFilterAttribute,
IExceptionFilter
从上面的代码中可以看到该特性可以应用在类,方法上面。在mvc中,就可以直接在控制器上面或者控制器中的Action上面直接使用,做到细粒度的对缓存的控制。
namespace OutputCacheDemo.Controllers
{
[OutputCache(Duration = )]
public class HomeController : Controller
{
// GET: Home
public string Index()
{
return DateTime.Now.ToString();
}
}
}
上面的代码是将OutputCache特性标记在了控制器类上,以达到该控制器上所有的Action都将应用该特性,过期时间设置为10s。10s后缓存过期,再访问就会更新时间。OutputCache特性也可以设置在Action方法上面,以达到更细粒度的控制缓存,代码如下:
public class HomeController : Controller
{
[OutputCache(Duration = )]
// GET: Home
public string Index()
{
return DateTime.Now.ToString();
}
}
此时,只有Index的页面进行了缓存。如果多个控制器或者Action使用相同的缓存配置,可以在配置文件中进行统一配置。
<system.web>
<caching>
<outputCacheSettings>
<outputCacheProfiles >
<add name='myoutputcache' duration=''/>
</outputCacheProfiles>
</outputCacheSettings>
</caching>
<compilation debug="true" targetFramework="4.5"/>
<httpRuntime targetFramework="4.5"/>
</system.web>
应用名称为myoutputcache的缓存代码如下:
public class HomeController : Controller
{
[OutputCache(CacheProfile = "myoutputcache")]
// GET: Home
public string Index()
{
return DateTime.Now.ToString();
}
}
注意:当控制器和Action同时使用了OutputCache特性时,以Action为主。
参考资料:https://www.cnblogs.com/wolf-sun/p/6219245.html
https://blog.csdn.net/zyh_1988/article/details/52234111
ASP.NET MVC学习系列(4)——MVC过滤器FilterAttribute的更多相关文章
- ASP.NET MVC学习系列(二)-WebAPI请求
继续接着上文 ASP.NET MVC学习系列(一)-WebAPI初探 来看看对于一般前台页面发起的get和post请求,我们在Web API中要如何来处理. 这里我使用Jquery 来发起异步请求实现 ...
- ASP.NET MVC学习系列(二)-WebAPI请求(转)
转自:http://www.cnblogs.com/babycool/p/3922738.html 继续接着上文 ASP.NET MVC学习系列(一)-WebAPI初探 来看看对于一般前台页面发起的g ...
- [转]ASP.NET MVC学习系列(二)-WebAPI请求 传参
[转]ASP.NET MVC学习系列(二)-WebAPI请求 传参 本文转自:http://www.cnblogs.com/babycool/p/3922738.html ASP.NET MVC学习系 ...
- ASP.NET MVC学习系列(二)-WebAPI请求 转载https://www.cnblogs.com/babycool/p/3922738.html
继续接着上文 ASP.NET MVC学习系列(一)-WebAPI初探 来看看对于一般前台页面发起的get和post请求,我们在Web API中要如何来处理. 这里我使用Jquery 来发起异步请求实现 ...
- MVC学习系列4--@helper辅助方法和用户自定义HTML方法
在HTML Helper,帮助类的帮助下,我们可以动态的创建HTML控件.HTML帮助类是在视图中,用来呈现HTML内容的.HTML帮助类是一个方法,它返回的是string类型的值. HTML帮助类, ...
- MVC学习系列——ModelBinder扩展
在MVC系统中,我们接受数据,运用的是ModelBinder 的技术. MVC学习系列——ActionResult扩展在这个系列中,我们自定义了XmlResult的返回结果. 那么是不是意味着能POS ...
- MVC学习系列——记一次失败面试后,感想。
在此写博客之际,热烈庆祝母校苏州科技学院,正式改名为苏州科技大学. 一晃眼,从自己投身IT行业已经两年有余,期间经历了结婚.买房等人生大事,非常感谢我的老婆,谢谢她这么爱我,嫁给我这个码农,呵呵... ...
- MVC学习一:MVC简单流程
MVC学习一:MVC初次接触 1.MVC简单流程 1.1.服务器接收客户端请求后,解析URL(根据 路由表里配置的URL来分析 类名(控制器名)和方法名)根据请求的类名,创建对应的控制器类对象,并调用 ...
- Asp.net MVC 学习系列(一)序
题外话 公司本月开始提供早餐服务,2块天一餐,包括粥,两个包(听说是利口福供应的),一个鸡蛋.良心企业.公司原本有一个内部订餐系统,用Delphi开发的,开发的人早就走光了,也没有留下什么文档,现在项 ...
随机推荐
- 通过Python、BeautifulSoup爬取Gitee热门开源项目
一.安装 1.通过requests 对响应内容进行处理,requests.get()方法会返回一个Response对象 pip install requests 2.beautifulSoup对网页解 ...
- 【Python3爬虫】微博用户爬虫
此次爬虫要实现的是爬取某个微博用户的关注和粉丝的用户公开基本信息,包括用户昵称.id.性别.所在地和其粉丝数量,然后将爬取下来的数据保存在MongoDB数据库中,最后再生成几个图表来简单分析一下我们得 ...
- 从零打卡leetcode之day 4--无重复最长字符串
题目描述: 给定一个字符串,找出不含有重复字符的最长子串的长度. 示例: 给定 "abcabcbb" ,没有重复字符的最长子串是 "abc" ,那么长度就是3. ...
- 构造方法、封装、关键字(this、static)和代码块的介绍
1.构造方法 1.1 构造方法与成员方法的区别 构造方法分为无参构造和有参构造,其中有参构造方法和无参构造方法为方法的重载关系. 构造方法在初始化一个类的对象时进行调用,它没有返回值,方法名与类名相同 ...
- RabbitMQ消息队列(四)-服务详细配置与日常监控管理
RabbitMQ服务管理 启动服务:rabbitmq-server -detached[ /usr/local/rabbitmq/sbin/rabbitmq-server -detached ] 查看 ...
- leetcode — single-number
/** * Source : https://oj.leetcode.com/problems/single-number/ * * * Given an array of integers, eve ...
- JSP知识点总结
一.jsp静态包含和动态包含的区别 在学习request对象的时候,我们曾经使用过request.getRequestDispatcher(String url).include(request,re ...
- MariaDB主从复制的逻辑与实现
一.关系型数据库的劣势 “关系型数据库:指采用了关系模型来组织数据的数据库,而关系模型指的就是二维表格模型,而一个关系型数据库就是由二维表及其之间的联系所组成的一个数据组织.”——Wiki 关系型数据 ...
- Tomcat多实例部署
前言 以前总是采用很Low的方式太同一台服务器上部署多个Web应用,步骤是这样的:Copy Tomcat目录-->更改conf/server.xml三个端口号----->部署war包--- ...
- [VsCode] 开发所使用的VsCode的插件
vscode 的插件 必须 Chinese (Simplified) Language Pack for Visual Studio Code Markdown Preview Enhanced De ...