title: Null object cannot be converted to a value type exception is thrown inside ProcessRequest Method
body: <p>I am able to get the CurrentSideId, I can say anywhere through the application. I have a class called MainClass. It has static <code>CurrentSideId</code> property. <code>MainClass.CurrentSiteId</code> returns the <code>SiteId</code> requested.</p>
  
  <pre><code>public class MainClass{
     public static int CurrentSiteId
     {
        get { return HttpContext.Current.Request.RequestContext.RouteData.Values["CurrentSiteId"].To&lt;int&gt;(); }
        set {..}
     }
  }
  </code></pre>
  
  <p>But, when it comes to <code>ProcessRequest</code> method of the <code>HttpHandler</code>, <code>CurrentSiteId</code> throws an exception of type <code>System.InvalidCastException</code>: <code>Null object cannot be converted to a value type.</code></p>
  
  <pre><code>public class MyRouteHandler : MvcRouteHandler
  {
      protected override IHttpHandler GetHttpHandler(RequestContext requestContext)
      {
          return new MyHttpHandler();
      }
  }
  public class MyHttpHandler : IHttpHandler
  {
      public void ProcessRequest(HttpContext context)
      {
          var siteId = MainClass.CurrentSiteId;
      }
  }
  </code></pre>
  
  <p>What would be the resolution?</p>