Rewrite HttpModule Mechanism

Source: Internet
Author: User

 

Recently, because SlickUpload is used, the upload effect is really good and the function is very strong. However, due to payment, a div layer is always displayed and redirected to its official website. So I came up with the idea of rewriting the HttpModule mechanism.

 

Anyone familiar with Asp. Net should know HttpModule, which is an important part in processing client requests. All client requests are required

 

After some preliminary processing, it is handed over to HttpHandler for final processing. HttpHandler calls ProcessRequest () to process the http request. After the processing is completed, the request is sent to the HttpModule. Then, the HttpModule forwards the processed Http request information to the client. The process is as follows:

 

Figure 1: Http request arrival HttpRuntime:

  

 

 

 

 

 

Figure 2: Process of HttpModule and HttpHandler:

 

   

 

 

 

 

 

The entire Http request processing process is: HttpRequest --> inetinfo.exe-> ASPNET_ISAPI.DLL --> HttpPipeline --> ASPNET_WP.EXE -->

 

HttpRuntime --> HttpApplication Factory --> HttpApplication --> HttpModule --> HttpHandler Factory --> HttpHandler. ProcessRequest ().

 

How HttpModule works: Listen to HttpRequest requests and add or filter some content to HttpRequest. HttpModule implements the IHttpModule interface. Rewrite the implementation of HttpModule to design a class that implements the IHttpModule interface. The IHttpModule interface includes two methods: 1. Dispose (). 2. Init (). The Code is as follows:

 

  

 

1 using System; 2 using System. collections. generic; 3 using System. linq; 4 using System. text; 5 using System. web; 6 7 namespace HttpModules 8 {9 public class ValidaterHttpModuleEvents: IHttpModule10 {11 # region IHttpModule Member 12 13 public void Dispose () 14 {15 throw new NotImplementedException (); 16} 17 18 public void Init (HttpApplication context) 19 {20 21 context. beginRequest + = new EventHandler (Application_BeginRequest); 22 context. endRequest + = new EventHandler (context_EndRequest); 23} 24 25 void context_EndRequest (object sender, EventArgs e) 26 {27 HttpContext. current. response. write ("ValidaterHttpModuleEvents EndRequest </br>"); 28} 29 30 # endregion31 32 void Application_BeginRequest (object sender, EventArgs e) 33 {34 35 HttpApplication application = sender as HttpApplication; 36 application. completeRequest (); 37 // HttpContext. current. response. redirect (" http://www.163.com "); 38 application. Context. Response. Write (" ValidaterHttpModuleEvents Begin Request </br> "); 39 40 41} 42} 43}

 

 

Then, add the following configuration code in the httpModules configuration section of System. Web in Web. Config:

 

<Add name = "HttpModule" type = "HttpModules. ValidaterHttpModuleEvents, HttpModules"/>

 

You can specify the name as needed. The Type is divided into two parts. The first part specifies the full name of the IHttpModule interface class, including the namespace and Type name, the later part specifies the Assembly after the Assembly is compiled successfully.

 

In this way, we can view the page in the browser as follows:

 

 

 

Similarly, if we rewrite the IHttpModule mechanism in the same way in multiple class libraries, you only need to add the following configuration code to the httpModules configuration section to add the same configuration section.

 

The code I re-wrote in another database is as follows:

 

  

 

Using System;

Using System. Collections. Generic;

Using System. Linq;

Using System. Text;

Using System. Web;

 

Namespace HttpModules2

{

Public class HttpModulesTest2: IHttpModule

{

# Region IHttpModule Member

 

Public void Dispose ()

{

}

 

Public void Init (HttpApplication context)

{

Context. BeginRequest + = new EventHandler (context_BeginRequest );

Context. EndRequest + = new EventHandler (context_EndRequest );

}

 

Public void context_EndRequest (object sender, EventArgs e)

{

HttpContext. Current. Response. Write ("HttpModulesTest2 EndRequest </br> ");

}

 

 

Public void context_BeginRequest (object sender, EventArgs e)

{

HttpContext. Current. Response. Write ("HttpModulesTest2 BeginRequest </br> ");

}

 

# Endregion

}

}

 

The configuration in the configuration file is as follows:

 

<Add name = "HttpModule" type = "HttpModules. ValidaterHttpModuleEvents, HttpModules"/>

<Add name = "HttpModuleTest2" type = "HttpModules2.HttpModulesTest2, HttpModules2"/>

 

The output is as follows:

 

ValidaterHttpModuleEvents Begin Request

ValidaterHttpModuleEvents EndRequest

HttpModulesTest2 EndRequest

 

Similarly: I rewrite an HttpModule in the same way, and the output result is similar. Later, I will provide the download code.

 

The output result is as follows: Multiple httpmodules process Http requests, only Web. the BeginRequest event of the first IHttpModule in the httpModules configuration section in Config will respond, but the EndRequest event of all IHttpModule instance objects in the httpModules configuration section will respond.

 

If you look at the link above, you will surely find that the execution method is that BeginRequest of all rewrite HttpModule mechanisms in the httpModules configuration section will be executed in the configuration order, then, the EndRequest is executed in the configuration order. According to the above example, we should:

 

1. HttpModule. BeginRequest

 

2. HttpModuleTest2.BeginRequest

 

3. HttpModule. EndRequest

 

4. HttpModuleTest2.EndRequest

 

In fact, according to the output in my example, this is not the case.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.