HTTP 处理程序与 HTTP 模块
有人能用不到两句话解释两者之间的区别吗?是的,我知道谷歌可以提供数百个答案,但不能提供二分之一的清晰句子:)
Can someone explain in less than 2 sentences the difference between both? Yes, I know google can provide hundreds of answers but not one in 2 clear sentences:)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
HttpHandler 是请求队列的去向。 HttpModule是沿途的一个站。
HttpHandler is where the request train is headed. HttpModule is a station along the way.
这两句话:
HttpModule 将针对应用程序的每个请求执行,无论扩展名如何,并且通常用于安全、统计、日志记录等。
HttpHandler 通常与特定扩展名相关联,并且用于诸如RSS 提要、动态图像生成或修改等。
如果这还不完全清楚,请多解释一下:
我对它们的看法 - 模块“插入”到请求管道,而处理程序“处理”特定的文件扩展名。因此,如果您有一个带有 LoggingModule 和 PdfHandler 的站点,则两者都会执行对 http:// 的请求example.com/sample.pdf,并且日志记录模块将单独执行对 http:// 的请求example.com/page.aspx。
MSDN 上有一篇关于差异的非常清晰的文章:HTTP 处理程序和 HTTP 模块概述
The two sentences:
An HttpModule will execute for every request to your application, regardless of extension, and is generally used for things like security, statistics, logging, etc.
An HttpHandler is generally associated with a specific extension, and is used for things like RSS feeds, dynamic image generation or modification, and the like.
A little more explanation if that's not completely clear:
The way I think about them - modules "plug in" to the request pipeline, whereas handlers "handle" a specific file extension. So, if you've got a site with a LoggingModule and a PdfHandler, both will execute for a request to http://example.com/sample.pdf, and the logging module alone will execute for a request to http://example.com/page.aspx.
There's a pretty clear article on the difference on MSDN: HTTP Handlers and HTTP Modules Overview
HttpHandler 和 HttpModule 的主要和共同目标是在 ASP.NET 请求到达 IIS 服务器之前注入预处理逻辑。
ASP.NET 提供了两种在请求管道中注入逻辑的方法;
Http Handler帮助我们根据请求的文件名的扩展名注入预处理逻辑。 ASP.NET 使用 HTTP 处理程序来实现许多自己的功能。例如,ASP.NET 使用处理程序来处理 .aspx、.asmx 和trace.axd 文件。
例子:
RSS 源:要为网站创建 RSS 源,您可以创建一个发出 RSS 格式 XML 的处理程序。因此,当用户向您的站点发送以 .rss 结尾的请求时,ASP.NET 会调用您的处理程序来处理该请求。
创建Handler涉及三个步骤
1. 实现IHttpHandler接口。
2. 在 web.config 或 machine.config 文件中注册处理程序。
3. 将文件扩展名(*.arshad)映射到IIS 中的aspnet_isapi.dll。
IHttpHandler 接口有 ProcessRequest 方法和 IsReusable 属性,需要实现。
ProcessRequest:在此方法中,您编写为处理程序生成输出的代码。
IsResuable:此属性指示此处理程序是否可以重用。
您可以像这样在 web.config 文件中注册处理程序
注意:这里我们处理任何扩展名为 arshad 的文件名。
HttpModule 是一个基于事件的处理器,用于在请求到达 IIS 服务器之前注入预处理逻辑。 ASP.NET 使用 HTTP 模块来实现许多自己的功能,如身份验证和授权、会话管理和输出缓存等。
当请求通过请求管道时,ASP.NET 引擎会发出大量事件。
其中一些事件包括 AuthenticateRequest、AuthorizeRequest、BeginRequest、EndRequest。
通过使用 HttpModule,您可以在这些事件中编写逻辑。这些逻辑在事件触发时、请求到达 IIS 之前执行。
创建模块涉及两个步骤,
1. 实现IHttpModule接口
2. 在 web.config 或 machine.config 文件中注册模块
示例:
安全性:使用 HTTP 模块,您可以在请求到达 IIS 之前执行自定义身份验证或其他安全检查。
The prime and common goal of HttpHandler and HttpModule is to inject pre-processing logic before the ASP.NET request reaches the IIS Server.
ASP.NET provides two ways of injecting logic in the request pipeline;
Http Handler helps us to inject pre-processing logic based on the extension of the file name requested. ASP.NET uses HTTP handlers for implementing a lot of its own functionality.For example, ASP.NET uses handlers for processing .aspx, .asmx and trace.axd files.
example:
RSS feeds: To create an RSS feed for a Web site, you can create a handler that emits RSS-formatted XML. So when users send a request to your site that ends in .rss, ASP.NET calls your handler to process the request.
There are three steps involved in creating Handler
1. Implement IHttpHandler interface.
2. Register handler in web.config or machine.config file.
3. Map the file extension (*.arshad) to aspnet_isapi.dll in the IIS.
IHttpHandler interface has ProcessRequest method and IsReusable property which needs to be implemented.
ProcessRequest: In this method, you write the code that produces the output for the handler.
IsResuable: This property tells whether this handler can be reused or not.
You can register the handler in web.config file like this
Note: here we are handling any file name with extension arshad.
HttpModule is an event based processor to inject pre-processing logic before the request reaches the IIS Server. ASP.NET uses HTTP Module to implement lots of its own functionality like authentication and authorization, session management and output caching etc.
ASP.NET engine emits lot of events as the request passess through the request pipeline.
Some of those events are AuthenticateRequest, AuthorizeRequest, BeginRequest, EndRequest.
By Using HttpModule you can write logic in these events. These logic get executed as the events fire and before the request reaches IIS.
There are two steps involved in creating Modules,
1. Implement IHttpModule interface
2. Register module in web.config or machine.config file
example:
Security: Using HTTP module, you can perform custom authentication or other security checks before the request reaches IIS.
HTTP 处理程序是响应对 ASP.NET Web 应用程序发出的请求而运行的进程。
HTTP 模块允许您检查传入和传出请求并根据请求采取操作。
HTTP handler is the process that runs in response to a request made to an ASP.NET Web application.
HTTP modules let you examine incoming and outgoing requests and take action based on the request.
HttpHandler 负责通过扩展处理 http 请求,而 HttpModule 则响应应用程序生命周期事件。
HttpHandler is responsible for handling http request by extension while HttpModule is responding to application life cycle events.
关于它的好文章 HttpModule-and-HttpHandlers
参考:信息:ASP.NET HTTP 模块和 HTTP 处理程序概述
“模块在处理程序执行之前和之后调用。模块使开发人员能够拦截、参与或修改每个单独的请求。处理程序用于处理各个端点请求。处理程序使 ASP.NET Framework 能够处理应用程序中的单个 HTTP URL 或 URL 扩展组。与模块不同,仅使用一个处理程序来处理请求”。
Nice article aboute it HttpModule-and-HttpHandlers
Reference: INFO: ASP.NET HTTP Modules and HTTP Handlers Overview
“Modules are called before and after the handler executes. Modules enable developers to intercept, participate in, or modify each individual request. Handlers are used to process individual endpoint requests. Handlers enable the ASP.NET Framework to process individual HTTP URLs or groups of URL extensions within an application. Unlike modules, only one handler is used to process a request”.
HTTP处理程序是根据设置实际完成编译的地方。例如如果页面扩展名是.aspx那么它将通过system.web.Ui.Pagahandlefactory进行编译。一旦编译完成,HTTP 句柄请求将通过 HTTP 模块和 IIS。
HTTP handler is where actually compilation is done based on setting. such as if page extension is .aspx then it will compile through system.web.Ui.Pagahandlefactory. once compilation is done at HTTP handle request will go though HTTP module and IIS.
HTTP 处理程序
HTTP 处理程序是响应 HTTP 请求而运行的进程。因此,每当用户请求文件时,处理程序都会根据扩展名对其进行处理。因此,当您需要基于文件扩展名进行特殊处理时,会创建自定义 http 处理程序。让我们考虑一个为站点创建 RSS 的示例。因此,创建一个生成 RSS 格式 XML 的处理程序。现在将 .rss 扩展名绑定到自定义处理程序。
HTTP 模块
HTTP 模块插入到请求的生命周期中。因此,当处理请求时,它会传递到请求管道中的所有模块。因此,http 模块通常用于:
安全性:用于在处理请求之前对请求进行身份验证。
统计和日志记录:由于每个请求都会调用模块,因此它们可用于收集统计数据和记录信息。
自定义标头:由于响应可以修改,因此可以向响应添加自定义标头信息。
HTTP Handler
HTTP Handler is the process which runs in response to a HTTP request. So whenever user requests a file it is processed by the handler based on the extension. So, custom http handlers are created when you need to special handling based on the file name extension. Let's consider an example to create RSS for a site. So, create a handler that generates RSS-formatted XML. Now bind the .rss extension to the custom handler.
HTTP Modules
HTTP Modules are plugged into the life cycle of a request. So when a request is processed it is passed through all the modules in the pipeline of the request. So generally http modules are used for:
Security: For authenticating a request before the request is handled.
Statistics and Logging: Since modules are called for every request they can be used for gathering statistics and for logging information.
Custom header: Since response can be modified, one can add custom header information to the response.