IHttpHandler 与 IHttpModule

发布于 2024-07-17 14:19:59 字数 323 浏览 8 评论 0原文

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

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

ゞ花落谁相伴 2024-07-24 14:19:59

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

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

  • RSS 源 要为网站创建 RSS 源,您可以创建一个发出 RSS 格式 XML 的处理程序。 然后,您可以将文件扩展名(例如 .rss)绑定到自定义处理程序。 当用户向您的站点发送以 .rss 结尾的请求时,ASP.NET 会调用您的处理程序来处理该请求。
  • 图像服务器 如果您希望 Web 应用程序提供各种尺寸的图像,您可以编写自定义处理程序来调整图像大小,然后将它们作为处理程序的响应发送给用户。

HTTP 模块是一个程序集,每次向应用程序发出请求时都会调用该程序集。 HTTP 模块作为 ASP.NET 请求管道的一部分进行调用,并且可以访问整个请求过程中的生命周期事件。 HTTP 模块允许您检查传入和传出请求并根据请求采取操作。

HTTP 模块的典型用途包括:

  • 安全性 因为您可以检查传入请求,所以 HTTP 模块可以在调用请求的页面、XML Web 服务或处理程序之前执行自定义身份验证或其他安全检查。 在以集成模式运行的 Internet Information Services (IIS) 7.0 中,您可以将表单身份验证扩展到应用程序中的所有内容类型。
  • 统计和日志记录 由于每个请求都会调用 HTTP 模块,因此您可以在集中模块中收集请求统计信息和日志信息,而不是在各个页面中。
  • 自定义页眉或页脚 由于您可以修改传出响应,因此您可以将自定义页眉信息等内容插入到每个页面或 XML Web 服务响应中。

来自: 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:

  • 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.

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:

  • 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.

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

浮光之海 2024-07-24 14:19:59

正如此处所述,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.

余生再见 2024-07-24 14:19:59

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

无论如何,最好将自定义逻辑放在单独的类中,然后只使用 IHttpModuleIHttpHandler 中的此类。 这样您就不必担心选择其中之一。 事实上,您可以创建一个额外的类来实现两者IHttpHandlerIHttpModule,然后通过在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 the IHttpModule.

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

够运 2024-07-24 14:19:59

15秒有一个不错的小教程给出实际示例

15 seconds has a nice small tutorial giving practical example

游魂 2024-07-24 14:19:59

模块旨在处理处理程序实际处理请求之前和之后应用程序引发的事件。 另一方面,处理程序没有机会订阅任何应用程序事件,而是简单地调用其 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文