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 </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?