Saturday, August 25, 2012

HttpHandlers in C#


In this post i want to write about HttpHandlers in C#.
An overview on how web requests are handled and executed by IIS
Generally whenever we request for a resource like localhost\MyWebApp\Firstpage.aspx
1. First of all the request reaches IIS and it identifies that this particular request should be handled by the .Net framework.
2. Next the request is handled by the worker process.
3. Next the worker process identifies the appropriate class that should handle this request.
This particular class is nothing but a HttpHandler. Now let us go through this.
HttpHandler
A HttpHandler is nothing but a class that handles incoming requests. Asp.Net framework identifies the appropriate handler based on the file extension in the request url.
IHttpHandler
A HttpHandler is nothing but a class that implements IHttpHandler interface. IHttpHandler has two methods.
1public interface IHttpHandler
2{
3    // Methods
4    void ProcessRequest(HttpContext context);
5
6    // Properties
7    bool IsReusable { get; }
8}
So any class implementing IHttphandler interface should define these two (ProcessRequest method and IsReusable property).
ProcessRequest should have the key logic to handle the http request. This is the method that is executed whenever a request comes.
Creating a class that implements IHttpHandler
This handler is called whenever a file ending in .nilesh is requested. A file with that extension does not need to exist.
01using System.Web;
02public class MyHandler : IHttpHandler
03{
04    public MyHandler()
05    {
06    }
07    public void ProcessRequest(HttpContext context)
08    {
09        System.Diagnostics.Debugger.Break();
10        HttpRequest Request = context.Request;
11        HttpResponse Response = context.Response;
12        
13
14

        Response.Write("Hi This is my first httphandler.");
16
17
18    }
19    public bool IsReusable
20    {
21        get return false; }
22    }
23}
IsReusable is used to specify whether we want to enable pooling or not. If set to true the handler is placed in the memory and it is pooled.
In the processRequest function i am just writing the text “Hi this is my first httphandler” as a response to the request we got.
So we are done with the creation of Httphandler.
Now you might be having a question. How does IIS know about this handler. How this particular request localhost\MySample\Test.nilesh is going to be redirected to our handler by IIS.
Now let me answer these questions.
1. We are supposed to configure the web.config file
Configuring Web.config For IIS7
i. There is a tag called Add, replace that with this tag or if not there add this tag.
1<add verb="*" path="*.nilesh" type="MyHandler" />
Here verb corresponds to the http protocol that our handler supports like ‘GET’,'POST’ etc
Path—The extension this particular handler should take care
Type—The class that handles the request. In our case MyHandler (This should be qualified name).
Mapping File extension to handlers in IIS7.0
1. Go to Command prompt and click Inetmgr.
2. create a website and host our website that contains Httphandler. I am naming my app as Test
Click on the HttpHandlerMapping in the below image
Now we can list of handler mappings in this image
Now let me click on .aspx and see to which handler that is actually pointing
Yes it is pointing to aspnet_isapi.dll. This is not a c# class but this will inturn process it and assign the aspx request to appropriate handler.
Now let us click on “Add Managed handler”
Fill the fields with appropriate values.
Requestpath—file extension to be handled.
Type—name of the class
Name—Name of the handler.
Now click on save.
Now go back to our solution and we can find that web.config is changed and few more tags were added to it
1<system.webServer>
2        <handlers accessPolicy="Read, Execute, Script">
3            <remove name="Test" />
4            <add name="MyHandler" path="*.nilesh" verb="GET,HEAD,POST,DEBUG" type="MyHandler"resourceType="Unspecified" requireAccess="Script" preCondition="integratedMode" />
5        </handlers>
6    </system.webServer>

These  tags we added automatically when we add mappings in iis.
The complete web.config file
01<configuration>
02    <system.web>
03        <compilation debug="true" />
04      <httpHandlers>
05        <add verb="*" path="*.nilesh" type="MyHandler" />
06      </httpHandlers>
07    </system.web>
08    <system.webServer>
09        <handlers accessPolicy="Read, Execute, Script">
10            <remove name="Test" />
11            <add name="MyHandler" path="*.nilesh" verb="GET,HEAD,POST,DEBUG" type="MyHandler"resourceType="Unspecified" requireAccess="Script" preCondition="integratedMode" />
12        </handlers>
13    </system.webServer>
14
15</configuration>
Testing our First Httphandler
Type this url in the browser “http://localhost:6192/samplewebservice/test.nilesh” and we can see the result on the screen.
Debugging Httphandler
if we want to debug the httphandler in our local machines , we can add this line
1System.Diagnostics.Debugger.Break();
Complete HttpHandler class code
01using System.Web;
02public class MyHandler : IHttpHandler
03{
04    public MyHandler()
05    {
06    }
07    public void ProcessRequest(HttpContext context)
08    {
09        System.Diagnostics.Debugger.Break();
10        HttpRequest Request = context.Request;
11        HttpResponse Response = context.Response;
12        // This handler is called whenever a file ending
13        // in .sample is requested. A file with that extension
14        // does not need to exist.
15        
16
17        Response.Write("Hello from a synchronous custom HTTP handler.");
18
19
20    }
21    public bool IsReusable
22    {
23        // To enable pooling, return true here.
24        // This keeps the handler in memory.
25        get return false; }
26    }
27}
In my next post i want to write about “How .asmx file request were handled”.


No comments:

Post a Comment