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-filters.aspx
Monday, January 17, 2011
ASP.NET MVC has action filters, while ASP.NET has HTTP modules. Inside their respective processing pipelines, these abstractions serve similar purposes, and I've heard the following question a few times:
When should I write an HTTP module and when should I write an action filter?
If you are creating an MVC application then I'll almost always recommend going with an action filter instead of an HTTP module. This is my recommendation even if the functionality you are creating is generic enough to work from inside a module (in other words, it doesn't depend on MVC specific pipeline stages, like the pre and post processing of action invocation). Filters are closely aligned with the MVC infrastructure and vocabulary, and filters are the first thing someone will look for when they want to see how you've implemented cross-cutting functionality in MVC.
On the other hand, an HTTP module is something you can reuse in any type of ASP.NET application. Witness the flexibility of elmah - just drop an assembly in the bin directory, tweak a config file, and wait for elmah to record errors across an entire application. This configurable flexibility isn't something you can do with filters out of the box, but we’ll see tomorrow how easy it is to add.
另外一个参考
from http://stackoverflow.com/questions/13550453/mvc-3-4-httpmodule-or-actionfilter
MVC is an abstraction over ASP.NET and therefore their "hooks" really depend at which level you want to inject your logic. An action filter will allow you to hook into MVC specific events:
OnActionExecuting– This method is called before a controller action is executed.OnActionExecuted– This method is called after a controller action is executed.OnResultExecuting– This method is called before a controller action result is executed.OnResultExecuted– This method is called after a controller action result is executed.
Whereas an HttpModule only allows you to hook into ASP.NET (upon which MVC is built) specific events:
BeginRequest- Request has been started. If you need to do something at the beginning of a request (for example, display advertisement banners at the top of each page), synchronize this event.AuthenticateRequest- If you want to plug in your own custom authentication scheme (for example, look up a user against a database to validate the password), build a module that synchronizes this event and authenticates the user in a way that you want to.AuthorizeRequest- This event is used internally to implement authorization mechanisms (for example, to store your access control lists (ACLs) in a database rather than in the file system). Although you can override this event, there are not many good reasons to do so.PreRequestHandlerExecute- This event occurs before the HTTP handler is executed.PostRequestHandlerExecute- This event occurs after the HTTP handler is executed.EndRequest- Request has been completed. You may want to build a debugging module that gathers information throughout the request and then writes the information to the page.
So it really depends on when you need to hook in your event and which events you need.
HTTP Modules versus ASP.NET MVC Action Filters的更多相关文章
- ASP.NET MVC : Action过滤器(Filtering)
http://www.cnblogs.com/QLeelulu/archive/2008/03/21/1117092.html ASP.NET MVC : Action过滤器(Filtering) 相 ...
- ASP.NET MVC Action返回结果类型【转】
ASP.NET MVC 目前一共提供了以下几种Action返回结果类型: 1.ActionResult(base) 2.ContentResult 3.EmptyResult 4.HttpUnauth ...
- ASP.NET MVC Action向视图传值之匿名类型
在使用ASP.NET MVC过程中想必大家都有遇到过一个问题就是我们的Action如何向视图传递匿名类型的值呢,如果不做特殊处理则无法实现. 接下来我们来看一个示例: 在我们的控制中: using S ...
- Asp.Net Mvc Action过滤器(二)
在Mvc中为Action添加过滤器,有两种方式, 一.使用ActionFilterAttribute,简单方式,同时支持Result的过滤处理, 1.可以为空,支持的重写:OnActionExecut ...
- ASP.Net MVC Action重定向跳出Controller和Area
1.重定向方法简介 [HttpPost] public ActionResult StudentList( string StudName, string studName, DateTime Bir ...
- Asp.net Mvc action返回多个模型实体给view
1.controller中action代码: public class HomeController : Controller { public ActionResult Detail(int id) ...
- Asp.net Mvc Action重定向总结
摘自博客园 程晓晖 [HttpPost] public ActionResult StudentList( string StudName, string studName, DateT ...
- ASP.NET MVC的Action Filter
一年前写了一篇短文ASP.NET MVC Action Filters,整理了Action Filter方面的资源,本篇文章详细的描述Action Filter.Action Filter作为一个可以 ...
- 使用ASP.NET MVC操作过滤器记录日志(转)
使用ASP.NET MVC操作过滤器记录日志 原文地址:http://www.singingeels.com/Articles/Logging_with_ASPNET_MVC_Action_Filte ...
随机推荐
- javax.servlet-api-xx.jar和servlet-api.jar区别
一.简介支持servlet的jar包.应该叫servlet-api.jar如果编写过servlet就知道要用到HttpServletRequest和HttpServletResponse等对象,这些对 ...
- js生成1~100随机数
(function (min,max){ var range = max - min; var rand = Math.random(); var num = min + Math.round(ran ...
- flask之redis
redis 连接需要host port passwod Hash:key-fields-value(做缓存)相当于一个key对于一个map,map中还有key-valueList:有顺序可重复(处理不 ...
- 使用Spring和Tomcat发布CXF SOAP WebService
上一节中使用代理工厂JaxWsProxyFactoryBean来发布WebService, 这种方式必须指定运行的端口,如果端口被占用,就会发布失败. cxf的WebService也可利用Tomcat ...
- selenium中Xpath轴定位方法
1.Xpath轴:轴可定义相对于当前节点的节点集. 使用语法:轴名称::节点名称 例://input[@data-value="SXRYNAME"]/parent::td/foll ...
- Unity Shader入门精要读书笔记(三)Shader必须的数学基础
Xyz三维坐标系如下:左手坐标系 但是摄像机观察空间则是采用右手系: 右手法则判断叉乘的结果的方向: 正交矩阵(单位互相垂直的基矢量构成正交矩阵)具有逆与转置一致性: 列矩阵运算CBAv和行矩阵的运算 ...
- MySQL优化方法论
MySQL优化方法 主机 操作系统 数据库 应用 MySQL优化理论 吞吐率(Throughput) VS 延时(Latency) 吞吐率: 我们一般使用单位时间内服务器处理的请求数来描述其并发处理能 ...
- NetBeans+Xdebug调试原理
使用Xdebug的远程调试,Xdebug作为一个嵌入到PHP的程序,扮演着客户端的角色,而IDE则作为服务器.下面的动态图展示了连接建立的过程. 服务端的IP为10.0.1.2, 使用HTTP协议,端 ...
- DDD学习笔录——提炼问题域之有效提炼知识的模型(三)
方式六:延迟对模型中概念的命名 对领域建模时命名很重要. 因为在不断的知识提炼过程中经常会发现已经被命名的概念与你最初理解的有出入,这时你当初的命名就会变成一个问题.其问题在于 最初选作名称的这个词 ...
- linux多线程默认栈大小和最大线程数
linux的线程栈大小可以使用ulimit -s查看,对于ubunt 2.6的内核线程栈的默认大小为8M,如下: shine@shine-bupt:~/Program/C$ ulimit -s 819 ...