HttpHandler and HttpModule in Real Scenario: Few Examples of HttpHandler

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 article we have learned to implement a simple HttpHandler in our ASP.NET application. If you are new to HttpHandler then you can visit our previous article by clicking the following link.

HttpHandler and HttpModule in Real Scenario: Getting Started with HttpHandler

In this article, we will understand a few more scenarios where we can implement HttpHandler. Let’s try to understand the following example.

Stop specific HTTP method in ASP.NET application

In this scenario, we can use HttpHandler. For example a situation might occur wherein we want to stop a certain HTTP verb in our application. In this scenario, we can use HttpHandler as a solution. In this example, we will stop a GET request in our application. When the user throws a GET request in the application our custom HttpHandler login will prevent the request by throwing one simple response message. Have a look at the following example.

This is the custom HttpHandler where we have written logic to prevent a GET request that will come from the user.

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Web;  
  5. namespace WebApp  
  6. {  
  7.     public class methodHandler : IHttpHandler  
  8.     {  
  9.         public void ProcessRequest(HttpContext context)  
  10.         {  
  11.             if(context.Request.RequestType.ToString() == "GET" || context.Request.HttpMethod.ToString() == "GET")  
  12.             {  
  13.                 context.Response.ContentType = "text/plain";  
  14.                 context.Response.Write("Please do not use GET request");  
  15.             }  
  16.          }  
  17.         public bool IsReusable  
  18.         {  
  19.             get  
  20.             {  
  21.                 return false;  
  22.             }  
  23.         }  
  24.     }  
  25. } 

We are checking the request type in the HttpContext object. If the request type is GET then we are displaying a custom response message from there.

We now need to register the HttpHandler in the web.config file in the application. This is the code to register the HttpHandler in the application.

We are giving path=”*.html”, in other words when any GET request is received from the user, our custom HttpHandler code will execute and it will prevent the GET request.

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

Here is the output of the example above. We are getting a custom message when we are making a GET request in the application.

Custom message

Not only GET, you can prevent any request type in the same way.

Determine whether or not the user is authenticated

This is another possible scenario wherein we can use HttpHandler. For example, in our example we will not allow any request from any unauthorized user.

We can check whether or not the user is authenticated by checking the “IsAuthenticated” property of the Request object. Have a look at the following example.  
  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.             //Check whether user is authenticated or not    
  12.             if (!context.Request.IsAuthenticated)    
  13.             {    
  14.                 context.Response.ContentType = "text/plain";    
  15.                 context.Response.Write("Please login to process request");    
  16.             }    
  17.          }    
  18.         public bool IsReusable    
  19.         {    
  20.             get    
  21.             {    
  22.                 return false;    
  23.             }    
  24.         }    
  25.     }    
  26. }

In this implementation, we are checking whether or not the user is authenticated. If he is not authenticated then we are dropping the request there and instead generating a custom response message.

We now need to register our HttpHandler in the web.config file. Here is the code to do that.

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

This is the example of the example above. We are seeing the custom response message because we did not implement any authentication mechanism in this example.

authentication

Check number of parameters in query string

Though this is not a significantly strong scenario where we can implement HttpHandle, but anyway, we can check the number of parameters in the query string for a certain page. For example we want to call a .aspx page where we need to send at least one parameter through query string to invoke the page. Before reaching the HTTP request to the specific page, we can check whether or not the user’s request is carrying a query string value.

Have a look at the following example.

In this example we are checking the length of the query string value in the HttpRequest, if the number of query strings is fewer than 1 then we will notify the user of the insufficient query string parameter.
  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("Form1.aspx"))  
  12.             {  
  13.                 if (context.Request.QueryString.Count < 1)  
  14.                 {  
  15.                     context.Response.ContentType = "text/plain";  
  16.                     context.Response.Write("Please provide at least one value through URL");  
  17.                 }  
  18.             }  
  19.         }  
  20.         public bool IsReusable  
  21.         {  
  22.             get  
  23.             {  
  24.                 return false;  
  25.             }  
  26.         }  
  27.     }  
  28. } 

This is the code to register the HttpHandler in the web.config file.

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

Here is the output; we will get it when the “Form1.aspx” page is requested.

Here is the output of the example above.

query string

Conclusion

In this article, we saw a few scenarios where we can implement a HttpHandler as a solution. Though those scenarios are not exact/perfect for HttpHandler to be implemented but those scenarios are enough to practice with HttpHandler. In a future article we will understand a little more realistic real-life concept of HttpHandler.


Similar Articles