April 16, 2011

Steps to create a HTTP Handler

Description:
    In this article I am just trying to writing down the steps with code example (C#) to create a simple custom HTTP handler. and configure it on IIS 5.1. The detail description about the HTTP Handler are described on this blog "HTTP Handlers (ASP.NET)"


Step 1: Creation of Handler:
     1. Create a web application (in this example i have created with name HTTPHandlerTest).
     2. Add a class to (I have used MyTestHandler in this example) your application and implement the IHttpHandler Interface. You can also implement the IRequireSessionState Interface if you want to access the session and IReadOnlySessionState if you want to access session data in read only mode.
     3. Implement the ProcessRequest() method and IsReusable Property. IsReusable property  determines whether the handler is pooled or not. If it returns true then the handler is kept on the pool for further user and the ProcessRequest() method to the processing.


    4. Modify the web.config file to register the handler. In this example i have registered the handler to process the the files having extension .test.



<httpHandlers>
    <add verb="*" path="*.test" type="HTTPHandlerTest.MyTestHandler, HTTPHandlerTest"/>
</httpHandlers>


Step 2: Configure the IIS 5.1
     1. Open the Internet Information Services (Control Panel--> Administrative Tool-->Internet Information Services).
     2. Go to "Default Web Site", Right click on it and open Properties.
     3. On the Properties window click on the "Home Directory" tab and click on the "Configuration" Button.
     4. Click on the "Add" button to register the new mapping. 
         In the Executable text box browse the path of the "aspnet_isapi.dll" files location on your machine (i.e. C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\aspnet_isapi.dll) and in the Extension text box enter the extension of the file that is going to process (i.e. i have used .test extension). Press OK to save setting.

Step 3: Testing Your Handler
    1. Add a file with extension .test to your application to test the application (I have added a file called MyTest.test to test it).
    2. Run the application and type "http://localhost:4611/MyTest.test" as URL.

No comments:

Post a Comment

Your comments are valuable.