0

I have the following to handle errors on an ASP.NET MVC 5 application:

protected void Application_Error() {

  var exception = Server.GetLastError();
  var httpException = exception as HttpException;
  Response.Clear();
  Server.ClearError();
  var routeData = new RouteData();
  routeData.Values["controller"] = "Errors";
  routeData.Values["action"] = "Internal";
  routeData.Values["exception"] = exception;
  Response.StatusCode = 500;
  if (httpException != null) {
    Response.StatusCode = httpException.GetHttpCode();
    switch (Response.StatusCode) {
      case 403:
        routeData.Values["action"] = "Forbidden";
        break;
      case 404:
        routeData.Values["action"] = "NotFound";
        break;
    }
  }
  // Avoid IIS7 getting in the middle
  Response.TrySkipIisCustomErrors = true;
  IController errorsController = new ErrorController();
  HttpContextWrapper wrapper = new HttpContextWrapper(Context);
  var rc = new RequestContext(wrapper, routeData);
  errorsController.Execute(rc);
}

This works fine but when I type, in the address bar, an unexistent url I don't get the 404 error.

I tried everything I could think of but no luck ...

Any idea why?

Thank You,

Miguel

Miguel Moura
  • 36,732
  • 85
  • 259
  • 481

1 Answers1

0
protected void Application_Error()
            {
                HttpContext httpContext = HttpContext.Current;
                if (httpContext != null)
                {
                    RequestContext requestContext = ((MvcHandler)httpContext.CurrentHandler).RequestContext;
                    /* when the  is ajaxRequest the system can automatically handle a mistake with a JSON response. then overwrites the default response */
                    if (requestContext.HttpContext.Request.IsAjaxRequest())
                    {
                        httpContext.Response.Clear();
                        string controllerName = requestContext.RouteData.GetRequiredString("controller");
                        IControllerFactory factory = ControllerBuilder.Current.GetControllerFactory();
                        IController controller = factory.CreateController(requestContext, controllerName);
                        ControllerContext controllerContext = new ControllerContext(requestContext, (ControllerBase)controller);

                        JsonResult jsonResult = new JsonResult();
                        jsonResult.Data = new { success = false, serverError = "500" };
                        jsonResult.JsonRequestBehavior = JsonRequestBehavior.AllowGet;
                        jsonResult.ExecuteResult(controllerContext);
                        httpContext.Response.End();
                    }
                    else
                    {
                      var httpException = requestContext.HttpContext.Server.GetLastError() as HttpException;
                      int errorCode = httpException.GetHttpCode();
                        switch(errorCode)
                        {
                            case 404:
                                 httpContext.Response.Redirect("~/Error404");
                                break;
                            default:
                                break;
                        }
                    }
                }
natnael88
  • 1,128
  • 10
  • 22