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