MVC Life Cycle - Part 3

Before reading this article, I highly recommend reading my previous parts:

Here, in this case, we will look at building a custom handler. HTTPHandlers implement IHTTPHandler and generate a response to HTTP-Request. Therefore, we first need to create the Handler and then register the same with the application either through code or via config file. IHTTPHandler exposes two properties.

  • IsReusable: IsReusable is just a flag which says whether the handler can be reused or not across requests.

  • ProcessRequest: ProcessRequest is the main execution method of the class which implements IHTTPHandler and also this is the one responsible for generating the response. One important point to note that a handler can respond to almost anything like plain-text, file, JSON, etc kind of requests. Now, without wasting time let us get started.

Now, let us look at the demo quickly.

  1. using System;    
  2. using System.Web;    
  3.     
  4. namespace MVC_Life_Cycle.CustomHandlers    
  5. {    
  6.     public class CustomHandler : IHttpHandler    
  7.     {    
  8.         public void ProcessRequest(HttpContext context)    
  9.         {    
  10.             throw new NotImplementedException();    
  11.         }    
  12.     
  13.         public bool IsReusable { get; }    
  14.     }    
  15. }  
Here, I have simply created one handler and implemented the interface. Currently, I have pasted the code in its raw format. Now, let me go ahead and modify the snippet.
  1. using System;    
  2. using System.Web;    
  3.     
  4. namespace MVC_Life_Cycle.CustomHandlers    
  5. {    
  6.     public class CustomHandler : IHttpHandler    
  7.     {    
  8.         public void ProcessRequest(HttpContext context)    
  9.         {    
  10.             context.Response.Redirect("http://myview.rahulnivi.net");    
  11.         }    
  12.     
  13.         //This is also correct    
  14.         //public bool IsReusable    
  15.         //{    
  16.         //    get { return false;}    
  17.         //}    
  18.     
  19.         //But I have used expression syntax here    
  20.         public bool IsReusable => false;    
  21.     }    
  22. }   
Next, we need to go ahead and register this handler. Here, I will do the same from the code as in the following code snippet:
  1. using System;    
  2. using System.Collections.Generic;    
  3. using System.Linq;    
  4. using System.Web;    
  5. using System.Web.DynamicData;    
  6. using System.Web.Mvc;    
  7. using System.Web.Routing;    
  8. using MVC_Life_Cycle.CustomHandlers;    
  9.     
  10. namespace MVC_Life_Cycle    
  11. {    
  12.     public class RouteConfig    
  13.     {    
  14.         public static void RegisterRoutes(RouteCollection routes)    
  15.         {    
  16.             routes.IgnoreRoute("{resource}.axd/{*pathInfo}");    
  17.     
  18.             routes.Add(new Route("home"new NewHandler()));    
  19.     
  20.             //Default Route is very greedy by nature. This will match almost any route. Hence, you need to put the new route above this.    
  21.             routes.MapRoute(    
  22.                 name: "Default",    
  23.                 url: "{controller}/{action}/{id}",    
  24.                 defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }    
  25.             );    
  26.         }    
  27.     }    
  28.     
  29.     public class NewHandler : IRouteHandler    
  30.     {    
  31.         public IHttpHandler GetHttpHandler(RequestContext requestContext)    
  32.         {    
  33.             return new CustomHandler();    
  34.         }    
  35.     }    
  36. }   
With the above change in place, when I run the app, then it will produce the home page as expected. But when I click on the Home link it will get redirected to my blog as shown below in the screenshot. Also, notice the series of events happening here.

Run Page

Code

My View

Therefore, the basic difference between HTTPHandlers and HTTPModule is there can be many modules serving one HTTP-Request. However, there is only one handler which will process the request and generate the HTTP-Response. Also, Handlers are implemented using IHTTPHandler and modules by IHTTPModule. I hope you have liked this discussion. We will delve further in coming sections.

Code Download Link.

 


Similar Articles