Create and register the custom httpmodules Module

Source: Internet
Author: User

Http://msdn.microsoft.com/zh-cn/ms227673.aspx

ASP. NET Walkthrough: Create and register a custom HTTP Module

This walkthrough demonstrates the basic functions of the custom HTTP module. For each request, the HTTP module must be called to respond to the beginrequest and endrequest events. Therefore, this module runs before and after processing the request.

If ASP. NET applicationProgramIs running in IIS 6.0, you can use the HTTP module to customize requests for ASP. NET Resources. These resources include ASP. net web page (. aspx files), Web Services (. asmx files), Asp. net handler (. and you have mapped to ASP. any file type of. net. If the ASP. NET application runs under IIS 7.0, you can use the HTTP module to customize requests for any resources provided by IIS. This includes not only ASP. NET Resources, but also HTML files (.htm or. html files) and graphic files. For more information, see ASP. NET application lifecycle overview in IIS 5.0 and 6.0 and ASP. NET application lifecycle overview in IIS 7.0.

The sample module in this topic adds a message to the requested ASP. NET webpage at the beginning of any HTTP request. After the page is processed, it will add another message. This module also contains the correspondingCodeTo ensure that it does not add text to any other file type requests.

Each event handler is written as a private method of this module. When a registered event is triggered, ASP. NET calls an appropriate handler in this module to write information to the ASP. NET webpage.

This walkthrough involves the following tasks:

    • How to create the HTTP module code.

    • How to register this module in the web. config file.

Prerequisites

To complete this drill, you must:

    • Visual Studio or visual web developer.

    • ASP. NET Website.

This walkthrough also assumes that you are using IIS 6.0 or IIS 7.0. However, even if you are running ASP. NET development server, you can see the functions of this module.

Create custom HTTP module class

First, you need to create a class file to implement this module.

Create custom HTTP module class
  1. If the website does not have the app_code folder, create one in the root directory of the site.

  2. In the app_code directory, create a class file named helloworldmodule. VB (for Visual Basic) or helloworldmodule. CS (for C.

    Note:

    In addition, you canHelloworldmoduleCreate a class library project, compile the project, and place the generated assembly in the bin directory of the Web application.

  3. Add the following code to the class file:

    Copy code in Visual Basic

     Imports Microsoft. VisualBasic Public   Class Helloworldmodule Implements Ihttpmodule Public   Readonly   Property Modulename () As [String] Get      Return   "Helloworldmodule"      End   Get     End   Property      'In the init function, register for httpapplication      'Events by adding your handlers.      Public   Sub Init ( Byval Application As Httpapplication )_ Implements Ihttpmodule. init Addhandler Application. beginrequest ,_ Addressof   Me . Application_beginrequest Addhandler Application. endrequest ,_ Addressof   Me . Application_endrequest End   Sub      Private   Sub Application_beginrequest ( Byval Source As   Object ,_ Byval E As Eventargs) 'Create httpapplication and httpcontext objects to access      'Request and response properties.     Dim Application As Httpapplication = ctype (source, _ httpapplication) Dim Context As Httpcontext = application. Context Dim Filepath As   String = Context. Request. filepath Dim Fileextension As   String = Virtualpathutility. getextension (filepath) If (Fileextension. Equals ( ". Aspx" )) Then Context. response. Write _( "<H1> <font color = Red> helloworldmodule :" &_ "Beginning of request </font>  ) End   If      End   Sub      Private   Sub Application_endrequest ( Byval Source As   Object ,_ Byval EAs Eventargs) Dim Application As Httpapplication = ctype (source, _ httpapplication) Dim Context As Httpcontext = application. Context Dim Filenameextension As   String = _ System. Io. Path. getextension (_ httpcontext. Current. Request. filepath) If Filenameextension = ". Aspx"   Then      Dim Filepath As   String = Context. Request. filepath Dim Fileextension As   String = Virtualpathutility. getextension (filepath) If (Fileextension. Equals ( ". Aspx" )) Then Context. response. Write _( "<HR>  &_ "End of request </font>  ) End  If      End   Sub      Public   Sub Dispose () Implements Ihttpmodule. Dispose End   Sub      End   Class     

     

    C # copy code

     Using System; Using System. Web; Public   Class Helloworldmodule: ihttpmodule {Public Helloworldmodule (){} Public String modulename { Get { Return   "Helloworldmodule" ;}} // In the init function, register for httpapplication      // Events by adding your handlers.      Public   Void Init (httpapplication application) {application. beginrequest + = ( New Eventhandler ( This . Application_beginrequest); application. endrequest + = ( New Eventhandler ( This . Application_endrequest ));} Private   Void Application_beginrequest (Object source, eventargs e ){ // Create httpapplication and httpcontext objects to access      // Request and response properties. Httpapplication application = (httpapplication) source; httpcontext context = application. context; String Filepath = context. Request. filepath; String Fileextension = virtualpathutility. getextension (filepath );If (Fileextension. Equals ( ". Aspx" ) {Context. response. Write ( "<H1> <font color = Red>" + "Helloworldmodule: Beginning of request" + "</Font>  );}} Private   Void Application_endrequest (Object source, eventargs e) {httpapplication application = (httpapplication) source; httpcontext context = application. context; String Filepath = context. Request. filepath; String Fileextension = virtualpathutility. getextension (filepath ); If (Fileextension. Equals ( ". Aspx" ) {Context. response. Write ( "<HR>  + "Helloworldmodule: End of request </font>  );}} Public   Void Dispose (){}}

  4. Save and close the class file.

  5. In"Generate"Click"Generate website".

    If the website is not generated, correct any problems. The custom HTTP module must be compiled; otherwise, the module cannot be registered.

Register the HTTP module in IIS 6.0 and IIS 7.0 classic modes

CreateHelloworldmoduleYou can create an item in the web. config file to register this module. By registering the HTTP module, you can subscribe to notifications in the request pipeline.

In IIS 7.0, applications can run in classic or integrated mode. In Classic mode, the request processing method is basically the same as that in IIS 6.0. In integration mode, IIS 7.0 manages requests using pipelines (pipelines allow it to share requests, modules, and other functions with ASP. NET.

In IIS 7.0 Classic mode and IIS 7.0 integration mode, the process of registering a module is different. This section describes the process that corresponds to the classic mode of IIS 6.0 and IIS 7.0. The next section describes the process of registering a module that runs in IIS 7.0 integration mode.

Register the module for IIS 6.0 and IIS 7.0 running in Classic mode.
    1. If the website does not have a web. config file, create one in the root directory of the site.

    2. Add the highlighted code below to the Web. config file:

      Copy code

      <Configuration> <system. Web> 

      Use this codeHelloworldmoduleTo register the module.

Register the HTTP module in iis7.0 integration mode

The process of registering a module in IIS 7.0 integration mode is slightly different from that in IIS 7.0 Classic mode.

Iis7.0 registration module running in integration mode
    1. If the website does not have a web. config file, create one in the root directory of the site.

    2. Add the highlighted code below to the Web. config file:

      Copy code

      <Configuration> <system. webserver> <modules> <Add name = "helloworldmodule" type = "helloworldmodule"/> </modules> </system. webserver> </configuration>

      Note:

      You can also use the IIS manager to register the module. For more information, see configuring ing modules in IIS 7.0 (configuring modules in IIS 7.0 ).

      Use this codeHelloworldmoduleTo register the module.

Test the custom HTTP Module

After creating and registering the custom HTTP module, you can test it.

Test the custom HTTP Module
    1. Add an ASP. NET page to the application.

    2. request the page in the browser.

      the HTTP module appends a string to the beginning and end of the response. This module runs automatically when any request is sent to an ASP. NET file with its extension. For more information, see HTTP handler and HTTP module overview.

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.