Walkthrough: Creating and Registering a Custom HTTP Module

Walkthrough: Creating and Registering a Custom HTTP Module

This walkthrough illustrates the basic functionality of a custom HTTP module. An HTTP module is called on every request in response to the BeginRequest and EndRequest events. As a result, the module runs before and after a request is processed.

If the ASP.NET application is running under IIS 6.0, you can use HTTP modules to customize requests for resources that are serviced by ASP.NET. This includes ASP.NET Web pages (.aspx files), Web services (.asmx files), ASP.NET handlers (.ashx files), and any file types that you have mapped to ASP.NET. If the ASP.NET application is running under IIS 7.0, you can use HTTP modules to customize requests for any resources that are served by IIS. This includes not just ASP.NET resources, but HTML files (.htm or .html files), graphics files, and so on. For more information, see ASP.NET Application Life Cycle Overview for IIS 5.0 and 6.0 and ASP.NET Application Life Cycle Overview for IIS 7.0.

The example module in this topic adds a message to the requested ASP.NET Web page at the beginning of any HTTP request. It adds another message after the page has been processed. The module includes code that makes sure that it does not add text to a request for any other file type.

Each event handler is written as a private method of the module. When the registered events are raised, ASP.NET calls the appropriate handler in the module, which writes information to the ASP.NET Web page.

Tasks illustrated in this walkthrough include the following:

  • How to create the code for an HTTP module.

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

Prequisites

In order to complete this walkthrough, you will need:

  • Visual Studio or Visual Web Developer.

The walkthrough also assumes that you are working with IIS 6.0 or IIS 7.0. However, you can see the functionality of the module even if you run the ASP.NET Development Server.

原文地址:https://www.cnblogs.com/chucklu/p/12720072.html