How can I catch the server error http status code in a general view of user errors in ASP.NET MVC

I am trying to implement a common custom error page in ASP.NET MVC 4. I basically set up an error layout that defines the section for outputting the http response status code.

The view in which I want my errors to end inherits from such a layout and simply adds a message that comes from its model, which was created and transmitted in the View () call in the controller (with the name "Error") I am configured to handle custom errors in the web.config file.

<customErrors defaultRedirect="/Error" mode="On"> </customErrors> 

User error controller :

 public class ErrorController : Controller { public ActionResult Index() { return View(new CustomErrorViewModel(Response.Status)); } } 

Custom error view :

 @{ Layout = "~/Views/Shared/_CustomErrorLayout.cshtml"; } @using System.Web.Configuration; @using System.Configuration; @using MVC_Tests.Models; @model CustomErrorViewModel @section HttpCode { @Response.StatusCode } @Model.Status 

Layout:

 <!DOCTYPE html> <html> <head> <meta name="viewport" content="width=device-width" /> <title></title> <link href="~/Content/CustomError.css" rel="stylesheet" /> </head> <body> <div id="divError"> <h1> <span>Error &nbsp;</span> @RenderSection("HttpCode") </h1> <div id="divErrorMessage"> <p>@RenderBody()</p> </div> </div> </body> </html> 

The error I'm trying to handle is a simple division by zero, which I added to the Home Index action. Please note: I want a single view to handle different HTTP server error codes (500 families). I know that I have different views for each individual http status code, I get one view for processing several codes.

Working with custom error handling works, I finish the desired view, but, as noted above, I was expecting to display an http status code about the error. Instead, I always show 200 (OK), which, according to my understanding and debugging, happens because:

1. First, an exception occurs after spontaneous division by zero.

2. Now redirection occurs because my web.config instructions handle errors in a custom way (since the specific http status code is not specified in the web.config file, I process each http status error code in the same controller / view ("/ Error") It is important here that the redirect is a new request.

3. The redirect from step 2 sends me to the error controller, and then sends me to its view, which is displayed.

4 .. When rendering the view, the http status code inserted in the section defined in the layout adds my custom message, and the output of part of the http status code is as I said: 200 (OK). Why, if a server-side error 500 was forcibly thrown? Since, if I'm not mistaken, the view is visualized in another request channel, redirected and redirected - there are no errors - and I get a response to state 200 (OK).

I looked at this Dino Esposito error handling message in asp.net mvc, but I don’t want to add filters (OnException and HandleError) for all application controllers (I could create a new base controller with such a filter and inherit all the others from it, but it also requires changing all controller classes). The last approach that he mentions (handling the global.asax Application_Error event), I'm not sure if this justifies itself - I have to issue a "Response.Redirect" redirect in the handler, which surpasses the flexibility of setting up custom pages in web.config (I'm still could redirect the route defined in the web.config file, but I'm wondering if this is getting too cumbersome for what looks so simple).

What is the best way to catch the HTTP response status code of any server error in my user error view?

+2
asp.net-mvc error-handling custom-errors
Mar 11 '15 at 14:15
source share
2 answers

If you are using IIS7 +, I would disable "customerrors" like this:

  <customErrors mode="Off" /> 

and use httpErrors as follows:

 <system.webServer> <httpErrors errorMode="DetailedLocalOnly" existingResponse="Replace" defaultPath="/error"> <remove statusCode="404" /> <error statusCode="404" path="/error/notfound" responseMode="ExecuteURL" /> </httpErrors> .... 

This allows you to display an error page that does not change the URL with a 404 status code using the following in the controller:

 public class ErrorController : Controller { public ActionResult notfound() { Response.TrySkipIisCustomErrors = true; Response.StatusCode = (int)HttpStatusCode.NotFound; return View("OneErrorViewToRuleThemAll"); } } 

ps. "errorMode" must be changed to "Custom" in order to see it locally .... depending on your local setting (ie if you use IISExpress), you may not see the correct error until you boot to the live server.

SFC. you can have a single view of "OneErrorViewToRuleThemAll" to handle the on-screen output of any status codes that you decide to process in this way. Each ErrorController action will end in this view.

+1
Mar 11 '15 at 16:05
source

The solution I was looking for (after I gave up) is described here .

Summarizing:

  • Set your web.config to redirect to controller action for every error you want to handle.
  • Set the TryToSkipCustomErrors property of the Response true object, in each action defined in 1.
  • Comment out the string filters.Add(new HandleErrorAttribute()); from filterConfig.cs file.

Using this approach, I finally managed to redirect to a user page with an error of 500. It seems that IIS at 500 by default is skipped by default, since setting up a custom page for error 404 does not require steps 2 or 3.

So, in the end, for each http status code I want to display a custom page, I create a controller action, set the described properties appropriately, and then redirect to the same view for all actions - where I show @ Response.Status, which makes Its a fairly universal solution for me.

+1
Mar 29 '15 at 9:15
source



All Articles