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.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace WebApp
{
public class countHandler : IHttpHandler
{
static int CountRequest = 0;
public void ProcessRequest(HttpContext context)
{
if (context.Request.RawUrl.Contains("Home.aspx"))
{
CountRequest++;
}
}
public bool IsReusable
{
get
{
return false;
}
}
}
}
The handler is realy simple and now we need to register the handler. Add the following code to your web.config file.
</system.webServer>
<handlers>
<add name="myImageHandler" verb="*" path="*.aspx" type="WebApp. countHandler"/>
</handlers>
</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.
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.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace WebApp
{
public class loginHandler : IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
if (context.Request.RawUrl.Contains("Home.aspx"))
{
context.Response.Redirect("abc.html");
}
}
public bool IsReusable
{
get
{
return false;
}
}
}
}
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.
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:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace WebApp
{
public class loginHandler : IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
if (context.Request.Cookies.Count > 0)
{
context.Response.ContentType = "text/plain";
context.Response.Write("Please don't send cookie in HTTP Request");
}
}
public bool IsReusable
{
get
{
return false;
}
}
}
}
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.
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 but for learning purposes and to make our concept clear those example might be helpful.