ASP.net: HttpHandlers

HTTPHandlers:

ASP.NET handles all the HTTP requests coming from the user and generates the appropriate response for it. ASP.NET framework knows how to process different kind of requests based on extension. For example, it can handle request for .aspx, .ascx and .txt files, etc. When it receives any request, it checks the extension to see if it can handle that request and performs some predefined steps to serve that request.

ASP.NET provides a low-level request/response API that enables developers to use .NET Framework classes to service incoming HTTP requests.So as a developer, One might want to handle some new kind of requests such as requests for .jpg or .gif. ASP.NET provides HTTPHandler to handle requests for .jpg or .gif.


An ASP.NET HTTP handler is the process or endpoint that runs in response to a request made to an ASP.NET Web application. The most common handler is an ASP.NET page handler that processes .aspx files. When users request an .aspx file, the request is processed by the page through the page handler. You can create your own HTTP handlers that render custom output to the browser.

Typical uses for custom HTTP handlers include the following:
  • RSS Feeds
  • Image Server
  • Transmit all .js files in one go in bundling form
  • Resource handling
HTTP handler features include the following:
  • The IHttpHandler interface is the starting point for developing handlers.
  • The IHttpAsyncHandler interface is the starting point for developing asynchronous handlers.
  • Custom handler source code can be put in the App_Code folder of an application, or it can be compiled and put in the Bin folder of an application.
  • Handlers developed for use in IIS 6.0 can be used in IIS 7.0 with little or no change. 

Creating a Custom HTTP Handler

To develop a customize handler, we should implement IHttpHandler interface, as show below.
 
public class ImageHandler :IHttpHandler
{
    public bool IsReusable
    {
        get { return false; }
    }

    public void ProcessRequest(HttpContext context)
    {

    }
}

The class should have a method ProcessRequest and a property called IsReusable. The property tells whether this handler can be reused or not and the method will be called whenever a request for that type comes.

public class ImageHandler :IHttpHandler
{
    public bool IsReusable
    {
        get { return false; }
    }

    public void ProcessRequest(HttpContext context)
    {
      //Set MIME Type
      context.Response.ContentType = "image/jpeg";
HTTPRequest oRequest = context.Request; 
HTTPResponse oResponse = context.Response;
 
if (oRequest.RawUrl.ToLower().Contains("nature.jpg")
{
oResponse.TransmitFile(oRequest.PhysicalApplicationPath +  "/images/nature.jpg");
} 
else
{
oResponse.Write("File Not Found");
}
}
}


Registering the Handler by Using Web.config

In IIS 6
<configuration>
  <system.Web>
    <httpHandlers>
      <add verb="*" path="*.jpg" type="ImageHandler"/>
    </httpHandlers>
  </system.Web>
</configuration> 
 
In IIS 7
<configuration>
  <system.WebServer>
    <handlers>
      <add verb="*" path="*.jpg" type="ImageHandler"/>
    </handlers>
  </system.WebServer>
</configuration> 
 
 
 
Configuring IIS to Forward Requests to ASP.NET 
For performance reasons, IIS passes only requests for specific file types to ASP.NET. For example, IIS passes requests for files with the .aspx, .axd, .ascx, and .asmx extensions to the Aspnet_Isapi.dll file that performs the ASP.NET processing. For all other file types, including HTML, JPG, and GIF, ASP.NET simply passes the file from the file system directly to the client browser.

The following steps outline the process for configuring
with IIS 7.0:

  1. Open the IIS Manager.
  2. Expand the nodes until you get to your site or to the default website. Select the node for your application.
  3. Double-click the Handler Mappings icon in the center pane of the IIS Manager. Notethat if you have used the Web.config method of registration, you should see that registration in the Handler Mappings list.
  4. In the Actions pane (on the right side), select Add Managed Handler.
  5. In the Add Managed Handler dialog box, shown in Figure 10-1, set Request Path tothe file name or extension you want to map; in this case, .jpg. The Type list allows youto choose the class name of the HTTP handler. The Name field is simply for entering a descriptive name.
  6.  

     

Comments

Popular posts from this blog

Data Bound Controls in ASP.Net - Part 4 (FormView and DetailsView controls)

The Clickjacking attack and X-Frame-Options