C# IHttpHandler 与 IHttpModule

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/769432/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-05 00:26:36  来源:igfitidea点击:

IHttpHandler vs IHttpModule

c#asp.netiishttphandlerhttpmodule

提问by Dan Esparza

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.

我的问题很简单(尽管答案很可能不是):我正在尝试决定如何在 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.

我已经使用了 HttpModules(IHttpModule 接口)和 HttpHandlers(IHttpHandler 接口),我发现我可以使用任何一种机制来实现它。我还发现我不明白两者之间的区别。

So my question is this: In what cases would I choose to use IHttpHandler instead of IHttpModule (and vice/versa)?

所以我的问题是: 在什么情况下我会选择使用 IHttpHandler 而不是 IHttpModule(反之亦然)?

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?

是否在管道中执行得更高?在某些情况下是否更容易配置?在中等安全性下不能很好地工作吗?

采纳答案by Ramani Sandeep

An ASP.NET HTTP handleris 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.

ASP.NET HTTP 处理程序是响应向 ASP.NET Web 应用程序发出的请求而运行的进程(通常称为“端点”)。最常见的处理程序是处理 .aspx 文件的 ASP.NET 页处理程序。当用户请求 .aspx 文件时,该请求由页面通过页面处理程序进行处理。您可以创建自己的 HTTP 处理程序,将自定义输出呈现给浏览器。

Typical uses for custom HTTP handlers include the following:

自定义 HTTP 处理程序的典型用途包括:

  • RSS feeds To create an RSS feed for a Web site, you can create a handler that emits RSS-formatted XML. You can then bind a file name extension such as .rss to the custom handler. When users send a request to your site that ends in .rss, ASP.NET calls your handler to process the request.
  • Image server If you want a Web application to serve images in a variety of sizes, you can write a custom handler to resize images and then send them to the user as the handler's response.
  • RSS 提要 要为网站创建 RSS 提要,您可以创建一个处理程序来发出 RSS 格式的 XML。然后,您可以将文件扩展名(例如 .rss)绑定到自定义处理程序。当用户向您的站点发送以 .rss 结尾的请求时,ASP.NET 会调用您的处理程序来处理该请求。
  • 图像服务器 如果您希望 Web 应用程序提供各种尺寸的图像,您可以编写一个自定义处理程序来调整图像的大小,然后将它们作为处理程序的响应发送给用户。

An HTTP moduleis 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.

一个HTTP模块是呼吁是你的应用的每个请求的组件。HTTP 模块作为 ASP.NET 请求管道的一部分被调用,并且可以访问整个请求中的生命周期事件。HTTP 模块允许您检查传入和传出的请求并根据请求采取行动。

Typical uses for HTTP modules include the following:

HTTP 模块的典型用途包括:

  • Security Because you can examine incoming requests, an HTTP module can perform custom authentication or other security checks before the requested page, XML Web service, or handler is called. In Internet Information Services (IIS) 7.0 running in Integrated mode, you can extend forms authentication to all content types in an application.
  • Statistics and logging Because HTTP modules are called on every request, you can gather request statistics and log information in a centralized module, instead of in individual pages.
  • Custom headers or footers Because you can modify the outgoing response, you can insert content such as custom header information into every page or XML Web service response.
  • 安全性 因为您可以检查传入的请求,所以 HTTP 模块可以在调用请求的页面、XML Web 服务或处理程序之前执行自定义身份验证或其他安全检查。在以集成模式运行的 Internet 信息服务 (IIS) 7.0 中,您可以将表单身份验证扩展到应用程序中的所有内容类型。
  • 统计和日志 因为每个请求都会调用 HTTP 模块,所以您可以在一个集中的模块中而不是在单个页面中收集请求统计和日志信息。
  • 自定义页眉或页脚 因为您可以修改传出响应,所以您可以将自定义页眉信息等内容插入到每个页面或 XML Web 服务响应中。

From: http://msdn.microsoft.com/en-us/library/bb398986.aspx

来自:http: //msdn.microsoft.com/en-us/library/bb398986.aspx

回答by Kirtan

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.

如前所述这里,的HttpModules是简单的类,可以自己插到请求处理管道,而从HttpHandlers的的HttpModules不仅是因为他们在请求处理管道位置的不同,而且还因为他们必须被映射到一个特定的文件扩展名。

回答by Igor Brejc

IHttpModulegives you much more control, you can basically control allof the traffic directed to your Web application. IHttpHandlergives you less control (the traffic is filtered beforeit reaches your handler), but if this is sufficient for your needs, then I see no reason to use the IHttpModule.

IHttpModule为您提供更多控制权,您基本上可以控制定向到您的 Web 应用程序的所有流量。IHttpHandler给您较少的控制(流量到达您的处理程序之前被过滤),但如果这足以满足您的需求,那么我认为没有理由使用IHttpModule.

Anyway, it's probably best to have your custom logic in a separate class, and then just use this class from either IHttpModuleor IHttpHandler. 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 bothIHttpHandlerand IHttpModuleand then decide what to use by setting it in Web.config.

无论如何,最好将您的自定义逻辑放在一个单独的类中,然后从IHttpModule或 中使用此类IHttpHandler。这样您就不必担心选择其中之一。事实上,你可以创建一个额外的类,它实现IHttpHandlerIHttpModule,然后再决定通过设置它使用什么Web.config

回答by Jason Diamond

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.

模块旨在处理应用程序在处理程序实际处理请求之前和之后引发的事件。另一方面,处理程序没有机会订阅任何应用程序事件,而是简单地调用它们的 ProcessRequest 方法,以便处理特定请求的“主要”工作。

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):

看看微软的这个文档(大约在“请求由 HttpApplication 管道处理”部分的页面下方):

http://msdn.microsoft.com/en-us/library/bb470252.aspx

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.

您可以在第 15 步中看到处理程序在哪里获得执行的机会。该步骤之前和之后的所有事件都可以被模块拦截,但不能被处理程序拦截。

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

http://www.brettle.com/neatupload

Here's a commercial one:

这是一个商业广告:

http://krystalware.com/Products/SlickUpload/

http://krystalware.com/Products/SlickUpload/

If you look at the documentation for NeatUpload, you'll see that it requires you to configure a module.

如果您查看 NeatUpload 的文档,您会发现它需要您配置一个模块。

回答by Yordan Georgiev

15 seconds has a nice small tutorialgiving practical example

15 seconds 有一个很好的小 教程给出了实际的例子