creating 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 :
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 f