title: Custom httphandler and routehandler with ASPNET MVC 4 and webapi
body: <p>I'm working on an ASPNET MVC 4 and WebApi. The webapi methods will be consumed by mobile devices. We need to secure the services and what we are using is to encrypt the data in some particular way.</p>
  
  <p>Now, I need to decrypt the call before the controller is reached. If the information decrypted is valid, it should continue to the controller as usual if not, I'll route the user to some error method.</p>
  
  <p>To accomplish this I think the best bet would be custom HttpHandler and custom RouteHandler. I'm following the tutorial <a href="http://geekswithblogs.net/sankarsan/archive/2009/01/18/developing-custom-routehandler.aspx" rel="noreferrer">here</a></p>
  
  <pre><code> public class MvcSecurityRouteHandler:IRouteHandler 
     { 
         public System.Web.IHttpHandler GetHttpHandler(RequestContext requestContext) 
         { 
            return new MvcSecurityHttpHandler(requestContext); 
         }
     }
  
  public class MvcSecurityHttpHandler : IHttpHandler, System.Web.SessionState.IRequiresSessionState, IRouteHandler
      {
          public RequestContext RequestContext { get; set; }
  
          public MvcSecurityHttpHandler(RequestContext requestContext)
          {
              this.RequestContext = requestContext;
          }
  
          public bool IsReusable
          {
              get { return true; }
          }
  
          public void ProcessRequest(HttpContext httpContext)
          {
              var controllerId = RequestContext.RouteData.GetRequiredString("controllerId");
              IController controller = null;
              IControllerFactory factory = null;
              try
              {
                  factory = ControllerBuilder.Current.GetControllerFactory();
                  controller = factory.CreateController(RequestContext, controllerId);
                  if (controller != null)
                  {
                      controller.Execute(RequestContext);
                  }
              }
              finally
              {
                  factory.ReleaseController(controller);
              }
  
              //string originalPath = httpContext.Request.Path;
              //HttpContext.Current.RewritePath(httpContext.Request.ApplicationPath, false);
              //IHttpHandler httpHandler = new MvcHttpHandler();
              //httpHandler.ProcessRequest(HttpContext.Current);
              //HttpContext.Current.RewritePath(originalPath, false);
          }
  
          public IHttpHandler GetHttpHandler(RequestContext requestContext)
          {
              throw new NotImplementedException();
          }
      }
  
  public class RouteConfig
      {
          public static void RegisterRoutes(RouteCollection routes)
          {
              routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
  
              var defaults = new RouteValueDictionary
                  {{"controllerId", "Home"},{"action", "Index"},{"id", string.Empty}};
  
              var customRoute = new Route("{controllerId}/{action}/{id}", defaults, new MvcSecurityRouteHandler());
              routes.Add(customRoute);
  
              routes.MapRoute(
                  name: "DefaultWebApi",
                  url: "{controller}/{action}/{id}",
                  defaults: new
                      {
                          controller = "Home",
                          action = "Index",
                          id = UrlParameter.Optional
                      });
          }
      }
  </code></pre>
  
  <p>Global.asax.cs</p>
  
  <pre><code> public static void RegisterRoutes(RouteCollection routes)
          {
              routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
  
              var defaults = new RouteValueDictionary
                  {{"controllerId", "Home"},{"action", "Index"},{"id", string.Empty}};
  
              var customRoute = new Route("{controllerId}/{action}/{id}", defaults, new MvcSecurityRouteHandler());
              routes.Add(customRoute);
          }
  </code></pre>
  
  <p>and in Application_Start</p>
  
  <pre><code>RegisterRoutes(RouteTable.Routes);
  </code></pre>
  
  <p>After the service is up, I create a breakpoint in ProcessRequest and it is not being hit.
  What could be missing? Is this the correct way of doing it?</p>