HttpModules and Http modules, httpmodules Module
HttpModules is used to add the HttpModule (http module) label to the current application. The configuration section is as follows:
I have to mention the Http request processing process when I mention the httpModule.
Process of ASP. NET processing requests:
When a * .aspxfile is requested, the request will be intercepted by the inetinfo.exe process. After determining the file suffix (aspx), it will forward the request to ASPNET_ISAPI.dll, ASPNET_ISAPI.dll sends the request to aspnet_wp.exein the http pipeline. The request is processed through HttpRuntime in the aspnet_wp.exe process. After the request is processed, the result is returned to the client.
Http Pipeline:
1. HttpRuntime transfers an Http request to HttpApplication. HttpApplication represents a Web application created by a programmer. HttpApplication creates an HttpContext object for this Http request. These objects contain many other objects related to this request, including HttpRequest, HttpResponse, and HttpSessionState. These objects can be accessed through the Page class or Context class in the program. ,
2. Next, the Http request uses a series of modules that have full control over the Http request. These modules can be used before a specific task is executed.
3. After the Http request passes through all modules, it will be processed by HttpHandler. In this step, perform some actual operations, usually the business logic completed on the. aspx page. You may not understand this process when creating the. aspx Page. However, you must know that the. aspx Page inherits from the Page class. Let's take a look at the Page class signature:
Public class Page: TemplateControl, IHttpHandler {// Code omitted}
As you can see, the Page class implements the IHttpHandler interface, and HttpHandler is also the lowest layer for Http request processing.
4. After HttpHandler completes processing, the Http request returns to the Module again. At this time, the Module can do some things after it has completed.
From step 2 to Step 4, you can use the following figure to show
Only one HttpHandler can be called during http request processing, but multiple httpmodules can be called.
The following describes HttpModule in detail.
The HTTP module is an assembly called every time a request is sent to an application. The HTTP module is called as part of the request pipeline. It can access lifecycle events throughout the request process. Therefore, the HTTP module allows you to check incoming requests and perform operations based on the requests. They also enable you to check the outgoing response and modify it.
The ASP. net http module calls all requests, which is similar to the ISAPI filter. However, they are written in managed code and can be fully integrated with the lifecycle of ASP. NET applications. You can place the source code of the custom module in the App_Code folder of the application, or the compiled custom module as an assembly in the Bin folder of the application.
ASP. NET uses modules to implement various application functions, including Forms authentication, caching, session status, and client script services. In each case, if these services are enabled, the module calls them as part of the request and executes tasks out of the scope of any single page request. The module can use application events, which may cause events that can be processed in the Global. asax file.
Function:
The module can subscribe to multiple request pipeline notifications. The module can receive event notifications from the HttpApplication object.
Use Cases
The HTTP module is generally used for the following purposes:
- Security because you can check incoming requests, the HTTP module can perform custom authentication or other security checks before calling the request page, XML Web services, or processing program. In Internet Information Service (IIS) 7.0 running in integrated mode, you can extend Forms authentication to all content types in the application.
- Statistics and logging because the HTTP module calls each request, you can collect request statistics and log information to a centralized module, instead of being collected to various pages.
- Because you can modify the output response, you can insert content, such as custom header information, into each page or XML Web services response.
Some built-in HttpModule in ASP. NET
Name |
Type |
Function |
OutputCache |
System. Web. Caching. OutputCacheModule |
Page-level output Cache |
Session |
System. Web. SessionState. SessionStateModule |
Session Status Management |
WindowsAuthentication |
System. Web. Security. WindowsAuthenticationModule |
Use integrated Windows authentication for client authentication |
FormsAuthentication |
System. Web. Security. FormsAuthenticationModule |
Client authentication using Cookie-based form Authentication |
PassportAuthentication |
System. Web. Security. PassportAuthenticationModule |
Customer identity verification with MS passport |
RoleManager |
System. Web. Security. RoleManagerModule |
Manage current user roles |
UrlAuthorization |
System. Web. Security. UrlAuthorizationModule |
Determine whether a user is authorized to access a URL |
FileAuthorization |
System. Web. Security. FileAuthorizationModule |
Determine whether a user is authorized to access a resource |
AnonymousIdentification |
System. Web. Security. AnonymousIdentificationModule |
Manage anonymous access in Asp. Net Applications |
Profile |
System. Web. Profile. ProfileModule |
Manage the creation of user archive files and related events |
ErrorHandlerModule |
System. Web. Mobile. ErrorHandlerModule |
Capture exceptions, format error message characters, and pass them to the client program |
The following is an example of customizing an HttpModule.
HttpModule must implement the interface System. Web. IHttpModule
namespace FastDoge.Study{ public class MyModule : IHttpModule { public void Dispose() { } public void Init(HttpApplication context) { context.BeginRequest += (new EventHandler(this.Application_BeginRequest)); context.EndRequest += (new EventHandler(this.Application_EndRequest)); } private void Application_BeginRequest(Object source, EventArgs e) { HttpApplication application = (HttpApplication)source; HttpContext context = application.Context; context.Response.Write("
Add the following configuration for Web. config running in IIS 6.0 and IIS 7.0 classic modes
<configuration> <system.web>
Because I am using the IIS7 integration mode, the configuration will be different.
<system.webServer> <modules> <add name="myModule" type="FastDoge.Study.MyModule"/> </modules> </system.webServer>
The effect is as follows:
Of course, the Website Cannot be empty. Otherwise, only one line is output.
<Hr>
. For more information about events that can be bound to the HttpApplication object passed in the Init method, see the events mentioned in the ASP. NET application lifecycle.
I found a picture on the Internet to list the sequence of event triggering.
The event between BeginRequest and PreRequestHandlerExecute is triggered before the server executes HttpHandler processing.
Events between PostRequestHandlerExecute and PreSendRequestContent are triggered after the server executes Handler processing.
However, the question is that Handler is called by an HTTP module built in ASP. NET. For ASP. NET WebForm, each Page is a Handler. For ASP. net mvc, what I saw before when I read Jiang Jinnan's old book is to trigger a UrlRoutingModule, which specifies an MvcHandler for HttpContext IN THE OnPostResolveRequestCache event. So that HttpHandler will be called? You may need to check the source code. Another thing worth learning is the addition of such modules and the registration of events to the Application, which makes HttpApplication more flexible.
I wrote a blog at the beginning and used HttpModule. At that time, a garden friend commented that Global. asax could be used. This was true in combination with the current scenario. However, there must be both advantages and disadvantages.
The module has the following advantages over the Global. asax file: the module can be encapsulated, so it can be used in many different applications after being created once.
The advantage of using the Global. asax file is that it can handle other registered events, such as Session_Start and Session_End. In addition, the Global. asax file allows you to instantiate Global objects that can be used throughout the application.
When you must create code that depends on application events and meet the following conditions, use the module:
- You want to reuse this module in other applications.
- Avoid placing complex code in the Global. asax file.
- This module applies to all requests in the MPs Queue (IIS 7.0 integration mode only ).
When you must create code that depends on application events and do not need to reuse code across applications, add the code to the Global. asax file.