Implement HttpHandler in ASP.Net Application

This is the “HttpHandler and HttpModule in real scenarios” article series. As the name suggests, in this series we will understand a few real scenarios where we can use HttpHandler and HttpModule. In our previous two articles we have learned how to implement a simple HttpHandler in our ASP.NET application. You can read them here.

In this article we will understand a few more scenarios where we can implement HttpHandler in an ASP.NET application.

Count number of request to certain page

Yes, there might be some other solution for this problem but we can use HttpHandler to count the number of hits to a certain page. Here is the implementation of trying to count the hits of a Home.aspx page. At first we are checking the request URL. If the URL contains “Home.aspx” then we will increment the count value by one.

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Web;  
  5. namespace WebApp  
  6. {  
  7.     public class countHandler : IHttpHandler  
  8.     {  
  9.         static int CountRequest = 0;  
  10.         public void ProcessRequest(HttpContext context)  
  11.         {  
  12.             if (context.Request.RawUrl.Contains("Home.aspx"))  
  13.             {  
  14.                 CountRequest++;  
  15.             }  
  16.         }  
  17.         public bool IsReusable  
  18.         {  
  19.             get  
  20.             {  
  21.                 return false;  
  22.             }  
  23.         }  
  24.     }  
  25. } 

The handler is realy simple and now we need to register the handler. Add the following code to your web.config file.

  1. <system.webServer>  
  2.   <handlers>  
  3.     <add name="myImageHandler" verb="*" path="*.aspx" type="WebApp. countHandler"/>  
  4.   </handlers>  
  5. </system.webServer> 

Ok, we have finished our implementation part. Now let’s try to call the Home.aspx page in a browser. After calling the Home.aspx page, our handler will execute and it will increase the “CountRequest” value by one.

We are seeing that now the CountRequest value is 2, because the output screen has taken in the second trial.

Count number of request

Redirect one page from other page

This is another possible scenario where we can implement HttpHandler as a solution. Let’s think about a situation where one web page exists but then it no longer exists but your client thinks that the page is still in the application.

Now to solve this problem we need to redirect the HTTP request of that page to some other page. For the redirect operation we can use HttpHandler. Have a look at the following code.

In this implementation, if someone tries to reach the Home.aspx page then she will redirect to the abc.html page. In this way we can implement redirection.

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Web;  
  5. namespace WebApp  
  6. {  
  7.     public class loginHandler : IHttpHandler  
  8.     {  
  9.         public void ProcessRequest(HttpContext context)  
  10.         {  
  11.             if (context.Request.RawUrl.Contains("Home.aspx"))  
  12.             {  
  13.                 context.Response.Redirect("abc.html");   
  14.             }  
  15.         }  
  16.         public bool IsReusable  
  17.         {  
  18.             get  
  19.             {  
  20.                 return false;  
  21.             }  
  22.         }  
  23.     }  
  24. } 

The registration of a HttpHandler is very similar to the previous example. Now if we try to reach the Home.aspx page then it will automatically be redirected to the abc.html page.

Redirect one page from other page

Check whether or not a cookie is present in the current HTTP request

This is another possible scenario. Again , there might be another solution for that but we can implement HttpHandler to detect cookies in HTTP requests.

The count property of the HttpContext class will tell the number of cookies in the current HTTP request. In this example we are detecting the cookie count, if the count is more than 0 then we will consider that the cookie is present in the current HTTP request and we will drop that request immediately.

Here is the implementation:
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Web;  
  5. namespace WebApp  
  6. {  
  7.     public class loginHandler : IHttpHandler  
  8.     {  
  9.         public void ProcessRequest(HttpContext context)  
  10.         {  
  11.             if (context.Request.Cookies.Count > 0)  
  12.             {  
  13.                 context.Response.ContentType = "text/plain";  
  14.                 context.Response.Write("Please don't send cookie in HTTP Request");  
  15.             }  
  16.         }  
  17.         public bool IsReusable  
  18.         {  
  19.             get  
  20.             {  
  21.                 return false;  
  22.             }  
  23.         }  
  24.     }  
  25. } 

The registration process of HttpHandler is similar to the previous example, there is no change in that. So that we are skipping that part.

When we will now try to browse the “home.aspx” page (or any .aspx page, because our handler will detect the .aspx page request). We will see the default message rather than the page contents. The reason is, we are sending the SessionId from cookies.

current HTTP request

Conclusion

In this article, we have tried to learn a few scenarios where we can implement HttpHandler. Though there might not be very strong reasons to use HttpHandler in those scenarios for learning purposes and to make our concept clear those examples might be helpful.


Similar Articles