How to fully boot using customErrors in Web.Config?

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.

 //GlobalConfiguration.Configure(WebApiConfig.Register); //FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters); 

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". <!-- Web.Config Configuration File --> <configuration> <system.web> <customErrors mode="RemoteOnly"/> </system.web> </configuration> 

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?

+1
source share
1 answer

Ok, thanks to the RoteS comment. I finally found what I need to do this!

The way I did this, performed the ErrorController, was not good.

 using (Controller controller = new ErrorController()) { ((IController)controller).Execute(new RequestContext(new HttpContextWrapper(httpContext), routeData)); } 

I found that instead of using ServerTransfert, we can get the customErrors attribute. Here is the final solution (verified):

 protected void Application_Error(object sender, EventArgs e) { // Response.TrySkipIisCustomErrors = true; I don't know if I will need it someday. var httpContext = ((HttpApplication)sender).Context; var exception = Server.GetLastError(); ErrorSignal.FromCurrentContext().Raise(exception); Server.ClearError(); Response.Clear(); string relativePath = "~/Administration/Error/{0}"; switch (Tools.GetHttpCode(exception)) { case (int)HttpStatusCode.BadRequest: Server.TransferRequest(string.Format(relativePath, "BadRequest")); break; case (int)HttpStatusCode.Unauthorized: Server.TransferRequest(string.Format(relativePath, "Unauthorized")); break; case (int)HttpStatusCode.Forbidden: Server.TransferRequest(string.Format(relativePath, "Forbidden")); break; case (int)HttpStatusCode.NotFound: Server.TransferRequest(string.Format(relativePath, "NotFound")); break; case (int)HttpStatusCode.InternalServerError: Server.TransferRequest(string.Format(relativePath, "ServerError")); break; default: Server.TransferRequest(string.Format(relativePath, "DefaultError")); break; } } 

Thanks to RoteS for the comment that pointed me in the right direction.

David

+1
source

Source: https://habr.com/ru/post/1204635/


All Articles