In-depth analysis of ASP. NET Learning Series-HttpModule (2)

Source: Internet
Author: User

  Preface

In the previous section, we discussed that using ISAPI is an important technology that allows us to expand server functions. There are two methods to achieve this: ISAPI extension and ISAPI filter, here we will talk about this ISAPI filter.

  Reading directory

I. ISAPI filter

Ii. HttpModule

Iii. Implementation of HttpModule

Iv. Running Mechanism of HttpModule

V. Running Effect

Vi. Running Effect

  I. ISAPI filter

Iis does not support dynamic pages, but also supports static. html page content.. asp ,. aspx ,. php ,. java and IIS do not process these tags. IIS treats these files as text and sends them to the client without processing. To solve this problem, IIS does not have this processing capability, IIS allows others to process it, that is, ISAPI filter, which is a COM component.

. Asp has its filter asp_isapi,. aspx has its filter aspnet_isapi, and php has its corresponding filter.

ASP. when the NET service registers to IIS, it registers each file extension that can be processed to IIS (for example :*. aspx ,*. ascx), these *. aspx ,*. ascx does not know how to handle IIS by default. If ASP is installed. NET and later, we need to let IIS know them, but IIS still does not know them, just put these *. aspx ,*. ascx is handed over to aspnet_isapi.dll for processing. After expansion, as long as you request. the aspx file will automatically jump to the special code processing process, that is, it will be handed to aspnet_isapi.dll for processing. After the aspnet_isapi.dll is processed, standard HTML code will be generated (for example, you have created a test. aspx, in test. aspx. the code in cs is Response. write ("test"), then after running, the word "test" will be output, which is the standard HTML code) and added to the original HTML code (for example: you have created a test. the HTML code in aspx is <bo Dy> </body>, which is the original HTML code. Combined, It is the <body> test </body>, which is the complete HTML code, finally, the complete HTML code is returned to IIS, and IIS sends the HTML code to the browser for explanation and execution. here we can see that IIS only recognizes static HTML code, some dynamic files are dedicated for processing ,. asp is asp_isapi ,. aspx is aspnet_isapi.

The following figure shows the installation of ASP. after the. NET environment, many files with the extension of others will be registered in IIS, where ASP. the files to be processed in the. NET environment are all processed by aspnet_isapi.

    

    

 Ii. HttpModule

HttpModule implements the ISAPI Filter function. We mentioned above that there are two methods to implement ISAPI extension: ISAPI extension, ISAPI Filter, and HttpModule ISAPI Filter, therefore, we can write HttpModule by ourselves, which inherits from System. web. IHttpModule interface.

The HttpModule implements System. web. IHttpModule interface.. NET components. These components register themselves in some events and insert themselves into the HTTP request processing pipeline. When these events occur, ASP.. NET calls the module that is interested in the request, so that the module can process the request.

    Iii. Implementation of HttpModule

3.1 compile a class that implements the System. Web. IHttpModule Interface

3.2 implement the Init method and register the required Method

3.3 Registration Method

      

3.4 register the written class in web. config

<HttpModules>
<! -- Add means to add the HttpModule class to the application. The name is not exquisite, And the type is exquisite. The namespace is in front of the comma. class Name. The comma is followed by the dll file name without a suffix -->
<Add name = "myHttpModule" type = "ClassLibrary. CustomHttpModule, ClassLibrary"/>
</HttpModules>

    Iv. Running Mechanism of HttpModule

As shown in the example. before Running aspx, run the Init () method in this module. Because an event handler is registered in the Init () method, the Init () authenticateRequest () event handler in the method.

These modules register themselves in some events and insert themselves into ASP. NET Request pipeline, when some events occur, ASP.. NET calls the Http module that is interested in the request, so that the module can process the request.

In summary, HttpModule processes a series of events of the HttpApplication object to influence the Http processing pipeline. These events are registered in the Init () method of HttpModule. The event execution sequence is as follows.

      

    V. Http request handling process

     It can be seen that HttpMoulde runs through HttpHandler before and after HttpHandler.

      

    Vi. Running Effect

We can see that no matter how many pages I create, as long as the request without parameters outputs "the user name or password is empty, cannot log on", even though all of my *. no code is written in the cs file, because the app. authenticateRequest + = new EventHandler (this. authenticateRequest); the event of this Code is the event of the arrival of the authentication request, which is the beginning of all events.

    

    

    

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.