User request application resource available in web server. IIS creates Http pipeline for the request processing.HttpContext contains request and response object.
Asp.net provides two ways of injecting pre processor logic in the request pipeline through HttpHandlers and HttpModules.
Introduction
We are going to discuss about implementing custom HttpHandlers in C#. HttpHandler help us to inject pre-processing logic based on the extension of the file name requested. So when a page is requested, HttpHandler executes on the base of extension file names and on the base of verbs. We can also map one handler to multiple file extensions. For instance, when any client requests for file with extension ‘GIF’ and ‘JPEG’, Testhandler pre-processing logic executes.Objective
In this article we are going to implement custom http handler using Generic Handler and write class for handler.Using the code
There are two ways to create custom http handler in VS 2010. They are
- Generic Handler Template
- Write class for http handler
Using the code
Below given example http handler invoked whenever *.gif file is requested by the user.
Using Generic Handler Template
VS 2010 provides Generic Handler template to create custom http handler.
Steps
1. Create Asp.net web application
2. Right click web application and select Add-->New Item option
3. Select "Generic Handler" template
4. Provide handler name in Name textbox
5. Handler file will be generated with ProcessRequest method and IsReusable property. Handler implements IHttpHandler interface.
6. Write your preprocessing logic in ProcessRequest Method.
7. IsReusable - Gets a value indicating whether another request can use the System.Web.IHttpHandler instance
8. Configuration Changes-->Open web.config file and add following elements
<httpHandlers> <add verb="*" path="*.gif" type="ClientWebApp.TestHandler, ClientWebApp"/> </httpHandlers> Below given example http handler invoked whenever *.gif file is requested by the user.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace ClientWebApp
{
/// <summary>
/// Summary description for TestHandler
/// </summary>
public class TestHandler : IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = "text/plain";
context.Response.Write("Hello World");
}
public bool IsReusable
{
get
{
return false;
}
}
}
}
Write class for Http Handler
We can create handler class that implements IHttpHandler. Above code can be used for creating handler using class file.
Steps
1. Create class called "TestHandler.cs" and implement IHttpHandler.
2. Create ProcessRequest method and IsReusable property.
3. Write your preprocessing logic in ProcessRequest Method.
4. IsReusable - Gets a value indicating whether another request can use the System.Web.IHttpHandler instance
5. Same configuration file changes can used for this approach as well. Make sure that namespace and class name is provided by type attribute.
Conclusion
Hope this article will help to create custom http handlers.
Happy Programming..