title: How to get error status code in ASP.NET MVC
body: <p>I am using ASP.NET MVC and the below code to return an exception description and store it in a session. I need help finding out the error code of the exception, like <code>500</code>, <code>403</code>, <code>404</code>, etc. How can I do that?</p>
  
  <p>This is how I am storing the error message in a session:</p>
  
  <pre><code>System.Web.HttpContext.Current.Session["errorMessage"] = filterContext.Exception.Message.ToString();
  
  public override void OnException(System.Web.Mvc.ExceptionContext filterContext)
  {
      ProcessResult processResult = new ProcessResult();
  
      // If the exception is already handled, we do nothing
      if (filterContext.ExceptionHandled)
      {
          return;
      }
      else
      {
          var controllerName = (string)filterContext.RouteData.Values["controller"];
          var actionName = (string)filterContext.RouteData.Values["action"];
          var model = new HandleErrorInfo(filterContext.Exception, controllerName, actionName);
          processResult.Controller = controllerName;
          processResult.Action = actionName;
          processResult.ExceptionMessage = filterContext.Exception.Message;
          System.Web.HttpContext.Current.Session["errorMessage"] = filterContext.Exception.Message.ToString();
          processResult.StackTrace = filterContext.Exception.StackTrace;
          processResult.Source = filterContext.Exception.Source;
      }
      // Mark the exception as handled
      filterContext.ExceptionHandled = true;
  }
  </code></pre>