博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
HTTP Modules versus ASP.NET MVC Action Filters
阅读量:5025 次
发布时间:2019-06-12

本文共 3270 字,大约阅读时间需要 10 分钟。

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 , while ASP.NET has . 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  - 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.

转载于:https://www.cnblogs.com/imust2008/p/5014018.html

你可能感兴趣的文章
golang 的编译安装以及supervisord部署
查看>>
easyui源码翻译1.32--Dialog(对话框窗口)
查看>>
阿里架构师,讲述基于微服务的软件架构模式
查看>>
Eclipse导入maven项目时,Pom.xml文件报错处理方法
查看>>
01、JAVA开发准备
查看>>
asp.net mvc 错误处理 - 自定义报错处理,生成错误日志
查看>>
Linux centos ssh
查看>>
R语言之避免for循环示例
查看>>
[转]jQuery 选择器和dom操作
查看>>
Jenkins+Maven+SVN快速搭建持续集成环境(转)
查看>>
bootstrap 媒体查询
查看>>
杜教筛
查看>>
《Ext JS模板与组件基本知识框架图----模板》
查看>>
txmpp
查看>>
微信开发时调用jssdk,在安卓设备中成功调用;在ios设备中返回错误消息:config fail,无其他具体错误消息,且接口权限显示获取ok,无法调用...
查看>>
【Github教程】史上最全github使用方法:github入门到精通
查看>>
抽象工厂模式(Abstract Factory)
查看>>
luogu1373 小a和uim之大逃离 (dp)
查看>>
Redis的Pub/Sub客户端实现
查看>>
SQL日常问题和技巧——持续更新
查看>>