. NET Framework Version: 1.0.3705 official version Vs. Net (C #) version: 7.0.9466 official version Just after dinner, just exercise your fingers on the keyboard. Next, continue to write this "Diary ":
Chapter Two -- how does httpmodule work? As we mentioned above, an HTTP request from the client is intercepted and transferred layer by layer (why are they playing the ball? Haha) the "request listener" of httpmodule is reached ". Httpmodule is similar to a *** installed in the aspnet_wp.exe process. People with a little knowledge can naturally imagine what it is for, and our httpmodule It can be said that it is a perfect candidate for ***, but it must be clear that httpmodule is definitely not a simple listener, it can do more, for example, it can add some content to intercepted requests. And so on. When an HTTP request arrives at the httpmodule, the ASP. NET Framework system does not actually process the request, but we can Before the HTTP request is passed to the real request processing center (httphandler), add the required information to the top of the HTTP request or to the HTTP Request information for some additional work, or in some cases simply terminate the HTTP request that meets some conditions, which can act as a filter, not just. By reading msdn (do not trust the quickstarts web document that comes with the. net sdk. The official version does not update this document in many places, and many things are invalid in the official version ), You will find that the system httpmodule implements an interface called ihttpmodule. Naturally, we should think that as long as our own class can implement the ihttpmodule interface, we will not be able to completely replace the system's Httpmodule? Completely correct. Before we start our own httpmodule class, let me tell you what the httpmodule looks like in the system. The default httpmodule in the ASP. NET system has the following: System. Web. caching. outputcachemodule System. Web. sessionstate. sessionstatemodule System. Web. Security. windowsauthenticationmodule System. Web. Security. formsauthenticationmodule System. Web. Security. passportauthenticationmodule System. Web. Security. urlauthorizationmodule System. Web. Security. fileauthorizationmodule
Now, let's start building our own httpmodule. 1) Open vs. Net to create a "class library" project and name it myhttpmodule. 2) reference the system. Web. dll file InCodeArea: Using system; Using system. Web; namespace myhttpmoduletest {< br> // // description: class used to implement custom httpmodule /// Author: uestc95 // contact: uestc95@263.net // public class myhttpmodule: ihttpmodule { // // description: constructor method // Author: uestc95 // contact: uestc95@263.net // Public myhttpmodule () {< br> }/// <Summary> /// Description: init method for implementing the ihttpmodule Interface /// Author: uestc95 // Contact: uestc95@263.net /// </Summary> /// <Param name = "application"> parameters of the httpapplication type </param> Public void Init (httpapplication Application) { Application. beginrequest + = new eventhandler (this. application_beginrequest ); Application. endrequest + = new eventhandler (this. application_endrequest ); } /// <Summary> /// Description: Private method defined by myself for doing something /// Author: uestc95 // Contact: uestc95@263.net /// </Summary> /// <Param name = "OBJ"> passed object parameters </param> /// <Param name = "E"> event parameters </param> Private void application_beginrequest (Object OBJ, eventargs E) { Httpapplication application = (httpapplication) OBJ; Httpcontext context = application. context; Httpresponse response = context. response; Httprequest request = context. request; Response. Write ("I'm from application_beginrequest ,:)");
} /// <Summary> /// Description: Private method defined by myself for doing something /// Author: uestc95 // Contact: uestc95@263.net /// </Summary> /// <Param name = "OBJ"> passed object parameters </param> /// <Param name = "E"> event parameters </param> Private void application_endrequest (Object OBJ, eventargs E) { Httpapplication application = (httpapplication) OBJ; Httpcontext context = application. context; Httpresponse response = context. response; Httprequest request = context. request; Response. Write ("I'm from application_endrequest ,:)");
} /// <Summary> /// Description: implements the ihttpmodule interface's dispose method. /// Author: uestc95 // Contact: uestc95@263.net /// </Summary> Public void dispose (){} } } 3) After compiling in vs. net, you will get the myhttpmodule. dll file. in the 4th Region, how can the aspnet_wp.exe process hand over the HTTP request to our own httpmodule? The method is to configure the Web. config file. on the web. add the following statements to the config file: be case sensitive because the web. config is case sensitive as an XML file. "Type = myhttpmoduletest. myhttpmodule, myhttpmodule" tells us that the system will send the HTTP request to the myhttpmoduletest. myhttpmodule class in the myhttpmodule. dll file for processing. The DLL file system will automatically Search for the bin subdirectory or the system's global Program set buffer zone (GAC. We can put the DLL file we just obtained in the bin subdirectory. As for the latter, you can use the. net sdk official version 's built-in config tool. We will not elaborate on it. Now, the httpmodule used to intercept the HTTP request is complete and the Assembly is complete. You can try to create a new webform in your web project and run it? :) Finally, let's assume that this httpmodule is used. Site A provides free ASP. net virtual space for everyone, but the administrator of Site A does not want to provide free lunch. When the page is browsed, the advertisement of my company is automatically displayed (just like www.x63.com). I cannot monitor all pages of all users at all times and want to add them manually on each page. For a piece of JS Code, the workload is unimaginable and unrealistic. Well, once our httpmodule is mounted, it will be easy, as long as we When the HTTP request is captured, just add some js to it! We mentioned above that two events beginrequest and endrequest are used in the init () method. These two events are the initial and final events of all the events that can be processed in Init (). There are some other events that can be used by us. You can refer to msdn. Before closing editplus, you need to enter the following: Response, request, server, and application can be used normally in httpmodule, but session-related Code cannot be operated! Why? Let's take a look at it for yourself. Next, let's take a look at the cause. In addition, let's ask another question. You can find out where the system's default httpmodules are configured? Find it.
Next, let's take a look at httphandler and how it works with httpmodule. See you later. |