web.config 关于HttpHandlers 和HttpModules地运用实际示例

vCreating HttpHandlers and HttpModules
Introduction
ASP.NET allows you to extend its functionality in two main ways : 

HttpHandlers
HttpModules
Http handlers are special applications that typically handle files with certain extension. For example when you request a file with extension .ASP IIS routes it to ASP processor. But what if I want to handle files with my own extensions say .bipin? Http handlers allow us to do just that. Now, you might be thinking what is the use of a new file extension? Consider a case where you want to generate graphics on the fly. In such cases you will write an Http handler that will do your task. Note that Http handlers are typically called for the specific file extensions they are designed for. If you have worked with ISAPI extensions in past you will find this concept very familiar.
Http modules are similar to Http handlers in that they allow you to tweak with the request and response. However, they are typically executed for every request irrespective of the file type. If you have worked with ISAPI filters before you will find this concept very familiar.

In this article we will see how to create Http Handlers and Http Modules and use them in your ASP.NET pages.

IHttpHandler interface
In order to create Http handler you have to create a class that implements IHttpHandler interface. This interface has one method with following signature :
Sub ProcessRequest(Context as HttpContext)
It also has one read only property with following signature :
Public ReadOnly Property IsReusable() As Boolean

The ProcessRequest method is used to do all of your processing. The context parameter provides access to various objects like Request and Response. The IsReusable property tells whether another requests can use the same instance of Http handler.

Creating the class implementing IHttpHandler
Following is a class that implements IHttpHandler interface.

Public Class MyHttpHandler    
Implements IHttpHandler     Public Sub ProcessRequest(ByVal context As HttpContext)             Implements IHttpHandler.ProcessRequest         context.Response.Write("hello world")     End     Sub Public ReadOnly Property IsReusable() As Boolean             Implements IHttpHandler.IsReusable         Get            Return True         End Get     End Property End Class
Here, we are simply outputting a string "Hello World" for each request handled by this Http handler. You can perform any complex task as per your requirements.

Configuring our Http handler
After you create your Http handler class you should configure your web application so that specific requests will be handled by the handler. To accomplish this you will modify web.config file as follows :

<httpHandlers>
<add verb="*" path="hello.ASPx"
type="SampleWebApplication.MyHttpHandler,SampleWebApplication" />
</httpHandlers>

Here, verb attribute indicates GET, POST or * (all). The path attribute  indicates the resource to be handled. In our case we have specific file hello.ASPx. Type attribute indicates the fully qualified name of the class and name of assembly respectively.

In case you have to handle different extension say *.bipin then in addition to configuring in web.config (as shown above) you also need to add the extension in IIS. This allows IIS to forward request for specific extension to ASP.NET processor which in turn forwards it to your Http handler.

Testing your http handler
In order to test your Http handler simply add a page named hello.ASPx in the project and run it in the browser. You should get "Hello world" displayed in your browser.

IHttpModule interface
In order to create a HttpModule you will first create a class that implements IHttpModule interface. This interface provides following two methods that you must implement :
Sub Init(ByVal app As HttpApplication)
Sub Dispose()
Out of the above two methods the Init() method is of our interest. This method receives an instance of HttpApplication that represents the current application instance. You will attach various event handlers in this method as we will see later on.
Creating the class implementing IHttpModule
Now, let us create a class that implements IHttpModule interface. Here is the complete code for the class :
Public Class MyHttpModule
    Implements IHttpModule

    Public Sub Init(ByVal app As HttpApplication)
    Implements IHttpModule.Init
        AddHandler app.BeginRequest, AddressOf MyBeginRequest
        AddHandler app.EndRequest, AddressOf MyEndRequest
    End Sub

    Public Sub Dispose() Implements IHttpModule.Dispose

    End Sub

    Public Sub MyBeginRequest
    (ByVal s As Object, ByVal e As EventArgs)
        Dim app As HttpApplication
        app = CType(s, HttpApplication)
        app.Response.Write("Hello begin request")
    End Sub

    Public Sub MyEndRequest
    (ByVal s As Object, ByVal e As EventArgs)
        Dim app As HttpApplication
        app = CType(s, HttpApplication)
        app.Response.Write("Hello end request")
    End Sub

End Class

 

Note how we have used Init() method to attach event handlers to application events. In our example we have set MyBeginRequest method to handle BeginRequest event of HttpApplication and MyEndRequest method handles EndRequest event. This will cause the every request to output "Hello begin request" and "Hello end request" at the start and end of the page respectively.
Add module details in web.config
Prior to using the module we just developed we must inform IIS and ASP.NET abut it. The place to do that is web.config file. Add following section to the file :
<httpModules>
    <add type="SampleWebApplication.MyHttpModule,
    SampleWebApplication"
    name="MyHttpModule" />
</httpModules>

 

The <httpModules> section is used to publish information about our module. All the modules from this section are loaded by ASP.NET at run time. The type attribute specifies the fully qualified class name and assembly name respectively.
Testing your http module
In order to test our module, create a test web form and put some controls on it. (Remember that if you use Grid layout our messages may not be displayed exactly at the beginning and end. For our testing switch to Flow layout). Now run the web form. You should see our messages at the top and bottom of the web form.

你可能感兴趣的:(handler)