I implemented my own error handler for my MVC5 project, and everything would be fine if it weren't for the customErrors attribute. I will explain: when I received an error in the application, I catch it inside the void Application_Error from Global.asax as follows:
protected void Application_Error(object sender, EventArgs e) { var httpContext = ((HttpApplication)sender).Context; ExecuteErrorController(httpContext, Server.GetLastError()); } public static void ExecuteErrorController(HttpContext httpContext, Exception exception) { if (!exception.Message.Contains("NotFound") && !exception.Message.Contains("ServerError")) { var routeData = new RouteData(); routeData.Values["area"] = "Administration"; routeData.Values["controller"] = "Error"; routeData.Values["action"] = "Insert"; routeData.Values["exception"] = exception; using (Controller controller = new ErrorController()) { ((IController)controller).Execute(new RequestContext(new HttpContextWrapper(httpContext), routeData)); } } }
Then inside my ErrorController I:
public ActionResult Insert(Exception exception) { ErrorSignal.FromCurrentContext().Raise(exception); Server.ClearError(); Response.Clear(); switch (Tools.GetHttpCode(exception)) // (int)HttpStatusCode.NotFound; { case 400: return RedirectToAction("BadRequest"); case 401: return RedirectToAction("Unauthorized"); case 403: return RedirectToAction("Forbidden"); case 404: return RedirectToAction("NotFound"); case 500: return RedirectToAction("ServerError"); default: return RedirectToAction("DefaultError"); } } public ActionResult Unauthorized() { return View(); } ...
So, the first time , everything works fine But !! The code repeats because the NotFound or ServerError page is not in the public folder. This page is supposed to be set in the customErrors attribute, but I don’t need the thing at all. I finally got this error: ERR_TOO_MANY_REDIRECTS because of this.
I read all day to find some answer about this, and this suggests that everyone who posted their code is doing the same image as mine, and no matter what I tried, nothing works.
Pay attention to my desperate condition: if (! Exception.Message.Contains ("NotFound") &! Exception.Message.Contains ("ServerError"))
I also comment on these two lines in global.asax, because wherever I read, he says that we need to delete them in order to do this.
Also, due to desperate if, I received this answer:
Runtime Error Description: An application error occurred on the server. The current custom error settings for this application prevent the details of the application error from being viewed. Details: To enable the details of this specific error message to be viewable on the local server machine, please create a <customErrors> tag within a "web.config" configuration file located in the root directory of the current web application. This <customErrors> tag should then have its "mode" attribute set to "RemoteOnly". To enable the details to be viewable on remote machines, please set "mode" to "Off". <!
I also tried Response.TrySkipIisCustomErrors = true; and it does not work!
So, how can I completely skate with customErrors and manage my own error handler in my project?