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 ...
随机推荐
- FastAdmin 一键 CRUD 生成时方法不存在的问题分析
FastAdmin 一键 CRUD 生成时方法不存在的问题分析 有群友反馈 使用 一键 CRUD 生成时不成功. 我试了以下命令 php think crud -t test -u 1 是成功的. 再 ...
- git超速掌握之一(基本使用)
前言: 无论你是运维.开发还是IT爱好者,都会听说github了吧?动不动哪位大神就说在github上有什么什么项目,我的github地址是xxxxx,甚至有自己个github在找新工作时都能给自己加 ...
- classpath和环境变量设置
一.简介: 环境变量是操作系统.应用程序.脚本程序等等的指明灯,能够告诉他们需要的资源在哪里.大多数的 系统都有一些预先设置好的环境变量,当然,我们也可以增加自己的环境变量. 为了看看当前系统的环境变 ...
- Linux下的Memcache安装,启动
一.linux安装memcache 1. 如果通过下载源码进行安装,则需要下载最新版本http://memcached.googlecode.com/files/memcached-1.4.13.ta ...
- Oracle日常性能问题查看
1 判断回滚段竞争的sql --当Ratio大于2时存在回滚段竞争,需要增加更多的回滚段) ratio from v$rollstat rs, v$rollname rn where rs.USN = ...
- SDRAM 之时序收敛(学习了特权老师)
到现在我还是不太理解SDRAM的时序设置,但是可能蒙对了.(呵呵) 开发环境: quartus II 13.0 板子: DE2 EP2C35F672C6N 时序约束step 1:create cl ...
- IDA Pro 权威指南学习笔记(一) - 启动 IDA
启动 IDA 启动 IDA,有一个欢迎界面 之后有一个对话框 选择 New 将启动一个对话框来选择将要分析的文件 选择 Go 将使 IDA 打开一个空白的工作区 如果要选择分析的文件,可以直接拖到 I ...
- java成神之——properties,lambda表达式,序列化
Properties 加载defaults.properties文件 写Properties到xml文件 读Properties从xml文件 Lambda表达式 自定义 内置 sort方法中使用Lam ...
- CRF++评测脚本CoNLL 2000
关于CRF++工具如何使用这里就不再赘述了,网上相关资源很多,如官方提供的http://crfpp.googlecode.com/svn/trunk/doc/index.html.虽然http://w ...
- Class.forName和ClassLoader.loadClass区别(转)
Java中class是如何加载到JVM中的:1.class加载到JVM中有三个步骤 装载:(loading)找到class对应的字节码文件. 连接:(linking)将对应的字节码文件读入 ...