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.
- using System;
- using System.Web;
- namespace MVC_Life_Cycle.CustomHandlers
- {
- public class CustomHandler : IHttpHandler
- {
- public void ProcessRequest(HttpContext context)
- {
- throw new NotImplementedException();
- }
- public bool IsReusable { get; }
- }
- }
- using System;
- using System.Web;
- namespace MVC_Life_Cycle.CustomHandlers
- {
- public class CustomHandler : IHttpHandler
- {
- public void ProcessRequest(HttpContext context)
- {
- context.Response.Redirect("http://myview.rahulnivi.net");
- }
- //This is also correct
- //public bool IsReusable
- //{
- // get { return false;}
- //}
- //But I have used expression syntax here
- public bool IsReusable => false;
- }
- }
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Web;
- using System.Web.DynamicData;
- using System.Web.Mvc;
- using System.Web.Routing;
- using MVC_Life_Cycle.CustomHandlers;
- namespace MVC_Life_Cycle
- {
- public class RouteConfig
- {
- public static void RegisterRoutes(RouteCollection routes)
- {
- routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
- routes.Add(new Route("home", new NewHandler()));
- //Default Route is very greedy by nature. This will match almost any route. Hence, you need to put the new route above this.
- routes.MapRoute(
- name: "Default",
- url: "{controller}/{action}/{id}",
- defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
- );
- }
- }
- public class NewHandler : IRouteHandler
- {
- public IHttpHandler GetHttpHandler(RequestContext requestContext)
- {
- return new CustomHandler();
- }
- }
- }



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.

Santhakumar MunuswamyPosted Jan 19, 2016, 2:54 PM
Thanks for nice article. Keep it up
Sthitaprajnya Debasis NayakPosted Jan 19, 2016, 9:13 AM
Nice Share !!!
Mohammed IbrahimPosted Jan 19, 2016, 8:57 AM
nice
Humayun Kabir MamunPosted Jan 19, 2016, 7:36 AM
Nice Rahul...