IHttpHandler 与 IHttpModule
我的问题很简单(尽管答案很可能不是):我正在尝试决定如何在 C# / ASP.NET 中实现服务器端上传处理程序。
我使用过 HttpModules(IHttpModule 接口)和 HttpHandlers(IHttpHandler 接口),我发现我可以使用任一机制来实现它。 我还发现我不明白两者之间的区别。
所以我的问题是:在什么情况下我会选择使用 IHttpHandler 而不是 IHttpModule(反之亦然)?
管道中的执行速度是否更高? 在某些情况下配置是否更容易? 中等安全性下效果不好吗?
My question is simple (although the answer will most likely not be): I'm trying to decide how to implement a server side upload handler in C# / ASP.NET.
I've used both HttpModules (IHttpModule interface) and HttpHandlers (IHttpHandler interface) and it occurs to me that I could implement this using either mechanism. It also occurs to me that I don't understand the differences between the two.
So my question is this: In what cases would I choose to use IHttpHandler instead of IHttpModule (and vice/versa)?
Is one executed much higher in the pipeline? Is one much easier to configure in certain situations? Does one not work well with medium security?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
ASP.NET HTTP 处理程序是为响应对 ASP.NET Web 应用程序发出的请求而运行的进程(通常称为“端点”)。 最常见的处理程序是处理 .aspx 文件的 ASP.NET 页面处理程序。 当用户请求 .aspx 文件时,该请求由页面通过页面处理程序进行处理。 您可以创建自己的 HTTP 处理程序,将自定义输出呈现到浏览器。
自定义 HTTP 处理程序的典型用途包括:
HTTP 模块是一个程序集,每次向应用程序发出请求时都会调用该程序集。 HTTP 模块作为 ASP.NET 请求管道的一部分进行调用,并且可以访问整个请求过程中的生命周期事件。 HTTP 模块允许您检查传入和传出请求并根据请求采取操作。
HTTP 模块的典型用途包括:
来自: http://msdn.microsoft.com/en-us/library/bb398986 .aspx
An ASP.NET HTTP handler is the process (frequently referred to as the "endpoint") that runs in response to a request made to an ASP.NET Web application. The most common handler is an ASP.NET page handler that processes .aspx files. When users request an .aspx file, the request is processed by the page through the page handler. You can create your own HTTP handlers that render custom output to the browser.
Typical uses for custom HTTP handlers include the following:
An HTTP module is an assembly that is called on every request that is made to your application. HTTP modules are called as part of the ASP.NET request pipeline and have access to life-cycle events throughout the request. HTTP modules let you examine incoming and outgoing requests and take action based on the request.
Typical uses for HTTP modules include the following:
From: http://msdn.microsoft.com/en-us/library/bb398986.aspx
正如此处所述,HttpModule 是可以将自身插入请求处理管道的简单类,而 HttpHandler 与 HttpModule 的不同之处不仅在于它们在请求处理管道中的位置,还因为它们必须映射到特定的文件扩展名。
As stated here, HttpModules are simple classes that can plug themselves into the request processing pipeline, whereas HttpHandlers differ from HttpModules not only because of their positions in the request processing pipeline, but also because they must be mapped to a specific file extensions.
IHttpModule
为您提供了更多控制权,您基本上可以控制定向到 Web 应用程序的所有流量。IHttpHandler
为您提供较少的控制(流量在到达您的处理程序之前被过滤),但如果这足以满足您的需求,那么我认为没有理由使用>IHttpModule
。无论如何,最好将自定义逻辑放在单独的类中,然后只使用
IHttpModule
或IHttpHandler
中的此类。 这样您就不必担心选择其中之一。 事实上,您可以创建一个额外的类来实现两者IHttpHandler
和IHttpModule
,然后通过在Web中设置它来决定使用什么.config
。IHttpModule
gives you much more control, you can basically control all of the traffic directed to your Web application.IHttpHandler
gives you less control (the traffic is filtered before it reaches your handler), but if this is sufficient for your needs, then I see no reason to use theIHttpModule
.Anyway, it's probably best to have your custom logic in a separate class, and then just use this class from either
IHttpModule
orIHttpHandler
. This way you don't really have to worry about choosing one or the other. In fact, you could create an extra class which implements bothIHttpHandler
andIHttpModule
and then decide what to use by setting it inWeb.config
.15秒有一个不错的小教程给出实际示例
15 seconds has a nice small tutorial giving practical example
模块旨在处理处理程序实际处理请求之前和之后应用程序引发的事件。 另一方面,处理程序没有机会订阅任何应用程序事件,而是简单地调用其 ProcessRequest 方法来完成处理特定请求的“主要”工作。
看一下 Microsoft 的此文档(大约在“请求由 HttpApplication 管道处理”部分的页面中间):
http://msdn.microsoft.com/en-us/library/bb470252.aspx
您可以在步骤 15 中看到处理程序有机会执行的位置。 该步骤之前和之后的所有事件都可以被模块拦截,但不能被处理程序拦截。
根据您想要实现的具体功能,您可以使用处理程序或模块来实现上传处理程序。 您甚至可能最终会同时使用两者。
需要考虑的事情可能是使用已经编写的上传处理程序。
这是一个免费的开源版本:
http://www.brettle.com/neatupload
这是一个商业版本:
http://krystalware.com/Products/SlickUpload/
如果您查看 NeatUpload 的文档,您会看到它需要您配置一个模块。
Modules are intended to handle events raised by the application before and after the request is actually processed by the handler. Handlers, on the other hand, aren't given the opportunity to subscribe to any application events and, instead, simply get their ProcessRequest method invoked in order to the "main" work of processing a specific request.
Take a look at this documentation from Microsoft (about half way down the page in the "The request is processed by the HttpApplication pipeline" section):
http://msdn.microsoft.com/en-us/library/bb470252.aspx
You can see in step 15 where the handler gets its chance to execute. All of the events before and after that step are available for interception by modules, but not handlers.
Depending on what specific features you're trying to achieve, you could use either a handler or a module to implement an upload handler. You might even end up using both.
Something to consider might to use an upload handler that's already written.
Here's a free and open source one:
http://www.brettle.com/neatupload
Here's a commercial one:
http://krystalware.com/Products/SlickUpload/
If you look at the documentation for NeatUpload, you'll see that it requires you to configure a module.