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的更多相关文章

  1. ASP.NET MVC : Action过滤器(Filtering)

    http://www.cnblogs.com/QLeelulu/archive/2008/03/21/1117092.html ASP.NET MVC : Action过滤器(Filtering) 相 ...

  2. ASP.NET MVC Action返回结果类型【转】

    ASP.NET MVC 目前一共提供了以下几种Action返回结果类型: 1.ActionResult(base) 2.ContentResult 3.EmptyResult 4.HttpUnauth ...

  3. ASP.NET MVC Action向视图传值之匿名类型

    在使用ASP.NET MVC过程中想必大家都有遇到过一个问题就是我们的Action如何向视图传递匿名类型的值呢,如果不做特殊处理则无法实现. 接下来我们来看一个示例: 在我们的控制中: using S ...

  4. Asp.Net Mvc Action过滤器(二)

    在Mvc中为Action添加过滤器,有两种方式, 一.使用ActionFilterAttribute,简单方式,同时支持Result的过滤处理, 1.可以为空,支持的重写:OnActionExecut ...

  5. ASP.Net MVC Action重定向跳出Controller和Area

    1.重定向方法简介 [HttpPost] public ActionResult StudentList( string StudName, string studName, DateTime Bir ...

  6. Asp.net Mvc action返回多个模型实体给view

    1.controller中action代码: public class HomeController : Controller { public ActionResult Detail(int id) ...

  7. Asp.net Mvc Action重定向总结

    摘自博客园 程晓晖 [HttpPost]        public ActionResult StudentList( string StudName, string studName, DateT ...

  8. ASP.NET MVC的Action Filter

    一年前写了一篇短文ASP.NET MVC Action Filters,整理了Action Filter方面的资源,本篇文章详细的描述Action Filter.Action Filter作为一个可以 ...

  9. 使用ASP.NET MVC操作过滤器记录日志(转)

    使用ASP.NET MVC操作过滤器记录日志 原文地址:http://www.singingeels.com/Articles/Logging_with_ASPNET_MVC_Action_Filte ...

随机推荐

  1. ZenCoding[Emmet]語法簡介【轉】

    快速指南 下面是一些常用的Zen Coding功能,目前VS2013的Web Essentials插件已经支持. '#' 创建一个id特性 '.' 创建一个类特性 '[]' 创建一个自定义特性 '&g ...

  2. subline自定义快捷键

    由于感觉原始subline的运行快捷键ctrl+b不是很方便,尝试修改,方法如下: 打开首选项 --> 快捷键设置 Key Bindings -Default //这个表示系统默认的快捷键.Ke ...

  3. CF 504E Misha and LCP on Tree——后缀数组+树链剖分

    题目:http://codeforces.com/contest/504/problem/E 树链剖分,把重链都接起来,且把每条重链的另一种方向的也都接上,在这个 2*n 的序列上跑后缀数组. 对于询 ...

  4. IPv6与IPv4最主要的不同

    IP第6个版本(IPv6),是互联网协议的新版本,设计为IP第4版本(IPv4,RFC-791)的继任.从IPv4升级到IPv6主要的改变有以下几类: 扩展地址容量 IPv6将IP地址的位址从32位提 ...

  5. 【转】Jmeter:图形界面压力测试工具

    Jmeter是一款强大的图形界面压力测试工具,完全用Java写成,关于Jmeter的介绍,网上其实有不少的文章,我原本是不想再重复写类似文章的,但我发现有些很关键性的,在我们测试中一定会用到的一些设置 ...

  6. Cassandra学习五 使用Key的正确姿势

    NoSQL一般是反范式的,比如提倡数据冗余,使得不至于写出非常复杂的SQL语句. Cassandra之中一共包含下面5中Key: Primary Key: 用来获取某一行的数据,可以是一列或多列   ...

  7. MFC学习(三)程序入口和执行流程

    1) WIN32 API程序当中,程序入口为WinMain函数,在这个函数当中我们完成注册窗口类,创建窗口,进入消息循环,最后由操作系统根据发送到程序窗口的消息调用程序窗口函数.而在MFC程序当中我们 ...

  8. pcs与crmsh命令比较

    一.概念 1.crmsh This project is not part of the GNU Project. Pacemaker command line interface for manag ...

  9. php写一个判断是否有cookie的脚本

    前言: 刚刚学习完cookie函数,写个练习. 0x01: //其实第二个应该改为elseif,但是我懒.啊哈 <?php $vlas="BnJhiFoPS4"; if(is ...

  10. angular结合echarts创建图表

    原理: 利用angularjs中的指令(directive)将echarts封装. 步骤: 1.封装函数: app.directive('line', function() { return { re ...