Learning ASP. NET Framework deep adventure (2)

Source: Internet
Author: User

I have been busy with the final exam these days and have no time to read books.

Position of httpmodule in ASP. NET Framework

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 common sense can naturally imagine what *** is used, our httpmodule can be said to be a perfect candidate for ***, but it must be clear that httpmodule is definitely not a simple listener, and it can do more, for example, it can add some content to intercepted requests. So where is the position of httpmodule in the entire ASP. NET Framework? Let's take a look at the figure below:

In addition, when an HTTP request arrives at httpmodule, the entire ASP. the net framework system does not actually process this HTTP request. That is to say, for an HTTP request, httpmodule is just a place where it passes. However, it is precisely because httpmodule is the "only way to go" for an HTTP request that we can transmit to the real request processing center (httphandler) in this HTTP request) add the required information to the HTTP request information or perform some additional work on the HTTP request information we have intercepted, or, in some cases, simply terminate an HTTP request that meets certain conditions to act as a filter, not just. Check msdn (do not trust it. 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, can we 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 the default httpmodule of these systems is in the file machine. config. The file is located in.. NET Framework directory, such as c: \ winnt \ microso in your system file directory. Ft. Net \ framework \ v1.0.3705 \ config \ machine. config. When we develop ASP. NET applications, we will frequently use a web. config configuration file. What is the relationship between this machine. config and our common web. config? Originally in ASP. when the Net Framework starts to process an HTTP request, it will load the machine in sequence. config and the Web. the configuration in the config file contains tags. You can also understand what it means. If you have configured an httpmodule in machine. config, you can still remove this ing from the Web. config file closest to you.

Build our own httpmodule
In the previous section, we talked about that each of the default httpmodules of the system inherits an interface called ihttpmodule. Let's take a look at the real face of this interface:
Syntax: public interface ihttpmodule
Requirement: namespace: system. Web
Platform: Windows 2000, Windows XP Professional, windows. NET Server family
Accessories: system. Web (in system. Web. dll)
Public Member method: void dispose (); parameter: no return value: void function: destroy resources no longer used by the module.
Void Init (httpapplication context); parameter: return value of an instance of the httpapplication type: void function: initialize a module to prepare for capturing HTTP requests.
After learning about the ihttpmodule method, we know how to implement it. Next, 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
{ /**/ /// 
///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 () {}  
/**/ ///
///Description: init method for implementing the ihttpmodule Interface
///Author: uestc95
///Contact: uestc95@263.net
/// 
///Parameters of the httpapplication type
Public   Void Init (httpapplication Application) {
Application. beginrequest+ =NewEventhandler (This. Application_beginrequest); application. endrequest+ =NewEventhandler (This. Application_endrequest );
}  
/**//// 
///Note: You have defined a private Method for doing something.
///Author: uestc95
///Contact: uestc95@263.net
/// 
///Passed object parameters
///Event Parameters
Private   Void Application_beginrequest (Object OBJ, eventargs E)
{< br> // declare httpapplication
httpapplication application = (httpapplicationobj;
httpcontext context = application. context; httpresponse response = context. response;
httprequest request = context. request; response. write ( " I'm from application_beginrequest, :) " );
}  
/**/ /// 
///Note: You have defined a private Method for doing something.
///Author: uestc95
///Contact: uestc95@263.net
/// 
///Passed object parameters
///Event Parameters
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 ,:)");}
/**/ // // note: implementation of the ihttpmodule interface's dispose method /// author: uestc95 /// contact: uestc95@263.net /// Public void dispose () {}
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.