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:
Pre-conditions
To complete this walkthrough, you need to:
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
Create an ASP. NET site and name it Handler.
Attention |
You can select any name for the Web site. |
If the Web site does not yet have an App_Code folder, create one in the root directory of the site.
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. |
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 () {}}
Save and close the class file.
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
-
If the site is not yet Web. config file, create one of these files at the root of the site.
-
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
If the Web site does not yet have Web. config files, create one such file at the root of the site.
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
Add a new ASP. NET page to the application.
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