Start Using HTTP Handlers In ASP.NET

Sometimes you need to handle a specific request separately with a different way which a normal request is going through. This article will explain HTTP Handler in a simple, let’s begin.

Section 1

First, let me explain the main purpose of using HTTPHandler.

Sometimes you need to handle a specific request separately using a different method from that of a normal request. This article will explain HTTP Handler in a simple way. Let’s begin:

How can you handle any incoming HTTP request with a path “AllUsers/User.gif” by returning “Hello From HTTP Handler”.

  1. Create a class file --  it could be MyHttpHandler.cs
  2. Let MyHttpHandler class inherit from IhttpHandler Interface.
  3. In MyHttpHandler class, implement all the methods which are found in IhttpHandler Interface.

You should do the following,

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Web;  
  5. namespace MyProject {  
  6.     public class MyHttpHandler: IHttpHandler {  
  7.         public bool IsReusable {  
  8.             get {  
  9.                 return true;  
  10.             }  
  11.         }  
  12.         public void ProcessRequest(HttpContext context) {  
  13.             HttpResponse response = context.Response;  
  14.             response.Write("Hello From Http Handler");  
  15.         }  
  16.     }  
  17. }  

The next step is to register the http handler in the web.config,

  1. <httpHandlers>  
  2.    <add verb="*" path="AllUsers/User.gif" type="MyProject.MyHttpHandler,MyProject"/>  
  3. </httpHandlers>  

The above code (point 4) should be added as a child of <system.web>

That's all. Now HTTP Handler is ready

 Http Handler

Section2

When using HTTP Handler with either MVC or WEB API, you will get the following error:

 Error

Because each MVC and WEB API are using routing to access methods within the controller, they will search the path and find that it is not found. To solve this problem you have two choices:

The first one is,

  • Create a class file --  it could be MyRouteHandler.cs
  • Let MyRouteHandlerclass inherit from IRouteHandlerInterface.
  • In MyRouteHandler class, implement all the methods which are found in IRouteHandlerInterface and return the MyHttpHandlerwhich are created in the previous section.

You should do the following,

  1. public class MyRouteHandler: IRouteHandler {  
  2.     public IHttpHandler GetHttpHandler(RequestContext requestContext) {  
  3.         return new MyHttpHandler();  
  4.     }  
  5. }  

In RouteConfig class which is related to MVC and WEP API and responsible for requests routing, add the following line code to RegisterRoutes method and before the Default MapRoute. 

  1. routes.Add(new Route("AllUsers/User.gif"new MyRouteHandler()));  

Drop the HTTP Handler registration from web.config, since calling HTTP Handler will be from RegisterRoutes method.

The Second Solution Is:

Let us go back to section 1. It is a straightforward solution: In RouteConfig class which is related to MVC and WEP API and responsible for requests routing, add the following line of code to the RegisterRoutes method and before the Default MapRoute. 

  1. routes.IgnoreRoute("AllUsers/User.gif");  
That's it.

Note
If you want to Handle all HTTP requests which have a specific extentaion such as gif you can using the following in web.config.
  1. <httpHandlers>  
  2.    <add verb="*" path="*.gif" type="MyProject.MyHttpHandler,MyProject"/>  
  3. </httpHandlers>