Walkthrough: Creating and Registering a custom HTTP module

Source: Internet
Author: User

This walkthrough demonstrates the basic functionality of a custom HTTP module. for each request, the HTTP module needs to be called in response to the beginrequest and EndRequest events. Therefore, the module runs before and after the request is processed.

if the ASP. NET application is Run under IIS 6.0, you can use the HTTP module to customize requests for resources that are provided by ASP.   These resources include ASP. NET Web pages (. aspx files), Web Services (. asmx files), ASP. ashx files, and any file types that you have mapped to ASP.   if ASP. Applications are running under IIS 7.0, you can use the HTTP module to customize requests for any resources that are provided by IIS.   This includes not only the ASP. Resources, including HTML files (. htm or. html files), graphics files, and so on. For more information, see &NBSP, Overview of the ASP. NET application life cycle for IIS 5.0 and 6.0, and   Overview of the ASP. NET application life cycle for IIS 7.0.

The sample module in this topic adds a message to the requested ASP. NET page at the beginning of any HTTP request. after the page has been processed, it will add another message. The module also contains the appropriate code to ensure that it does not add text to requests for any other file type.

Each event handler is written as a private method of the module. when a registered event is raised, ASP. NET invokes the appropriate handler in the module to write information to the ASP.

This walkthrough demonstrates the following tasks:

    • How to create the code for the HTTP module.

    • How to register the module in the Web. config file.

Pre-conditions

To complete this walkthrough, you need to:

    • Visual Studio or Visual Web Developer.

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

To Create a custom HTTP module class

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

To create a custom HTTP module class
  1. Create an ASP. NET site and name it Handler.

    Attention

    You can select any name for the Web site.

  2. If the Web site does not yet have an App_Code folder, create one in the root directory of the site.

  3. In the App_Code directory, create a class file named Helloworldmodule.vb (for Visual Basic) or named HelloWorldModule.cs (for C #).

    Attention

    Also, if you are using visual Studio (instead of Visual Web Developer Express), you can create HelloWorldModule as a class library project, compile it, and place the resulting assembly in the Bi of your WEB application n the directory.

  4. Add the following code to the class file:

    usingSystem;usingsystem.web; Public classhelloworldmodule:ihttpmodule{ PublicHelloWorldModule () {} PublicString ModuleName {Get{return "HelloWorldModule"; } }    //in the Init function, register for HttpApplication//events by adding your handlers.     Public voidInit (HttpApplication application) {application. BeginRequest+=             (NewEventHandler ( This.        Application_BeginRequest)); Application. EndRequest+=             (NewEventHandler ( This.    Application_EndRequest)); }    Private voidApplication_BeginRequest (Object source, EventArgs e) {//Create HttpApplication and HttpContext objects to access//request and Response properties.HttpApplication application =(HttpApplication) source; HttpContext context=application.        Context; stringFilePath =context.        Request.filepath; stringFileExtension =virtualpathutility.getextension (FilePath); if(Fileextension.equals (". aspx") {context. Response.Write (""+"helloworldmodule:beginning of Request"+"</font>"); }    }    Private voidApplication_EndRequest (Object source, EventArgs e) {HttpApplication application=(HttpApplication) source; HttpContext context=application.        Context; stringFilePath =context.        Request.filepath; stringFileExtension =virtualpathutility.getextension (FilePath); if(Fileextension.equals (". aspx") {context. Response.Write (""+"Helloworldmodule:end of Request</font>"); }    }     Public voidDispose () {}}

  5. Save and close the class file.

  6. on the Build menu, click Build Web site.

    If the site is not generated, correct any problems that exist. the custom HTTP module must be compiled, or the module cannot be registered.

registering HTTP modules in IIS 6.0 and IIS 7.0 Classic mode

after you create a HelloWorldModule class, you can register the module by creating an entry in the Web. config file. by registering an HTTP module, you enable it to subscribe to request pipeline notifications.

In IIS 7.0, applications can run in Classic mode or Integrated mode. in Classic mode, requests are handled in the same way as in IIS 6.0. in Integrated mode, IIS 7.0 uses pipelines (pipelines make it possible to share requests, modules, and other features with ASP.) to manage requests.

In IIS 7.0 Classic mode and IIS 7 in Integrated mode, the process of registering a module differs. This section describes the procedures that correspond to the Classic mode of IIS 6.0 and IIS 7.0. The next section describes the procedures for registering modules that are running in IIS 7 Integrated mode.

Registering modules for IIS 6.0 and IIS 7.0 that are running in Classic mode
    1. If the site is not yet Web. config file, create one of these files at the root of the site.

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

       <configuration> <system.web>   " Type="  helloworldmodule   "/> 

       

      This code with  

registering the HTTP module in IIS7.0 Integrated mode

The process of registering a module in IIS 7 Integrated mode is slightly different from the registration process in IIS 7.0 Classic mode.

Registering modules for IIS7.0 running in Integrated mode
  1. If the Web site does not yet have Web. config files, create one such file at the root of the site.

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

    <configuration>  <system.webServer>    <modules>      <add name="  HelloWorldModule "type="helloworldmodule"/>    </modules>  </system.webServer></configuration>

    Attention

    You can also register a module by using IIS Manager. For more information, see Configuring Modules in IIS 7.0 (configuring modules in IIS 7.0).

    This code registers the module with the HelloWorldModule class name and module name.

Testing Custom HTTP modules

Once you have created and registered your custom HTTP module, you can test it.

Testing Custom HTTP Modules
    1. Add a new ASP. NET page to the application.

    2. Right-click the page you just added and select view in Browser.

      The HTTP module appends a string to the beginning and end of the response. the module will run automatically during any request to a file whose extension is assigned to ASP. For more information, see HTTP Handlers and HTTP Module overview.

Walkthrough: Creating and Registering a custom HTTP module

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.