HTTP Modules versus ASP.NET MVC Action Filters
2015-12-02 19:51
766 查看
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:
Whereas an HttpModule only allows you to hook into ASP.NET (upon which MVC is built) specific events:
So it really depends on when you need to hook in your event and which events you need.
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.
相关文章推荐
- iOS UIImageView 网络加载图片自适应大小问题
- HTTP Analyzer过滤器使用
- Android中开辟线程(下载-网络连接)
- [国嵌攻略][068][tftp网络协议实现]
- 【python】网络编程-TCP协议套接字
- 推荐一些C#相关的网站、资源和书籍 (转载自http://www.cnblogs.com/jiangxiaofan/p/3808316.html)
- 【python】网络编程-套接字常用函数
- Http、TCP/IP协议与Socket之间的区别
- AFN(上传、下载)
- 服务器搭建时报错:HTTP 错误 404.2
- c#使用HttpWebRequest上传文件同时携带其他参数
- Ubuntu12.10编译MPTCP
- 第二章 OSI参考模型和TCP/IP模型(续)
- 【图解HTTP】第一章 了解web及网络基础
- (转)tcp和udp能否发送0字节的数据包
- 网络模拟工具
- 【网络爬虫】数据的采集——Heritrix1.x的配置过程
- 各种HTTP返回错误代码大全 HTTP 403 Forbidden / 404 Not Found / 500 Internal Server Error / 502 Bad Gateway / 50
- Nginx httpS server配置
- VMware下打开Chrome OS遇到没有网络连接可用