Web creating httphandlers and HttpModules
Introduction
ASP.net allows to extend it functionality in two main ways:
httphandlers
HttpModules
Http handlers are special applications that typically the files with handle certain. For example if 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? Http handlers allow us to does just that. Now, you are might be thinking what are the use of a new file extension? Consider a case where your want to generate graphics on the fly. In such cases you'll write an Http handler that'll do your task. Note This Http handlers are typically called for the specific file extensions they are for. If you are have worked with ISAPI extensions in past your'll find this concept very.
The HTTP modules are similar to HTTP handlers in-they allow with the request and tweak. However, they are typically executed for every request irrespective of the file type. If you are have worked with an ISAPI filters before you'll find this concept very.
In this article we'll be there to create HTTP handlers and HTTP Modules and use them in your asp.net pages.
IHttpHandler interface
In the order to create Http handler your have to create a class of that implements IHttpHandler interface. This is interface has one method with following signature:
Sub ProcessRequest (context as HttpContext)
It also has one read with following signature:
Public ReadOnly Property IsReusable () as Boolean
The ProcessRequest method is used to does 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.PR Ocessrequest context. Response.Write ("Hello World") end Sub public ReadOnly Property isreusable () as Boolean Implements Ihtt Phandler.isreusable get "return True" End "End" Class
Here we are are simply outputting a string "Hello world" for each request handled by this Http handler. Can perform any complex tasks as per your requirements.
Configuring our Http Handler
After your create your Http handler class you should configure your Web application so specific requests would be Handl Ed by the handler. To accomplish this is modify Web.config file as follows:
Here, verb attributes 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 are have to handle different extension say *.bipin then into addition to configuring in Web.config (as shown above) You are also need to add the extension in IIS. This is allows IIS to forward the request for specific extension to ASP.net processor which in turn forwards it to your Http hand Ler.
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 are should get "Hello world" displayed in your browser.
IHttpModule interface
In the order to create a HttpModule the implements IHttpModule interface. This interface provides following two methods-must:
Sub Init (ByVal app as HttpApplication)
Sub Dispose ()
Out of the above two methods the "Init" () is are of our interest. This method receives a instance of HttpApplication that represents the current application instance. You'll attach various event handlers in this method as we'll have a later on.
Creating the class implementing IHttpModule
Now, let us create a class of that implements IHttpModule interface. Here's 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
How do we have used Init () method to attach event handlers to application events. In our example we have set mybeginrequest the to handle beginrequest event of HttpApplication and Myendrequest method h Andles endrequest event. This would 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 it. This is Web.config file. Add following section to the file:
<add type= "Samplewebapplication.myhttpmodule,
Samplewebapplication "
Name= "Myhttpmodule"/>
The Testing your HTTP Module
In order to test we module, create a test Web form and put some controls on it. (Remember that if your use Grid layout We messages May is displayed exactly at the beginning and end. For our testing switch to flow layout). Now run the Web form. You should the Web form in the top and bottom.
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.