Home How to make custom error pages work in ASP.NET MVC 4

# How to make custom error pages work in ASP.NET MVC 4

Water Cooler v2
1#
Water Cooler v2 Published in 2012-12-16 20:23:52Z
 I want a custom error page shown for 500, 404 and 403. Here's what I have done: Enabled custom errors in the web.config as follows:   Registered HandleErrorAttribute as a global action filter in the FilterConfig class as follows: public static void RegisterGlobalFilters(GlobalFilterCollection filters) { filters.Add(new CustomHandleErrorAttribute()); filters.Add(new AuthorizeAttribute()); }  Created a custom error page for each of the above messages. The default one for 500 was already available out of the box. Declared in each custom error page view that the model for the page is System.Web.Mvc.HandleErrorInfo For 500, it shows the custom error page. For others, it doesn't. Is there something I am missing? It does look like this is not all there is to displaying custom errors as I read through the code in the OnException method of the HandleErrorAttribute class and it is handling only 500. What do I have to do to handle other errors?
Kyrylo M
2#
Kyrylo M Reply to 2017-02-23 13:26:20Z
 My current setup (on MVC3, but I think it still applies) relies on having an ErrorController, so I use:   And the controller contains the following: public class ErrorController : Controller { public ViewResult Index() { return View("Error"); } public ViewResult NotFound() { Response.StatusCode = 404; //you may want to set this to 200 return View("NotFound"); } }  And the views just the way you implement them. I tend to add a bit of logic though, to show the stack trace and error information if the application is in debug mode. So Error.cshtml looks something like this: @model System.Web.Mvc.HandleErrorInfo @{ Layout = "_Layout.cshtml"; ViewBag.Title = "Error"; }
Error
@if (Model != null && HttpContext.Current.IsDebuggingEnabled) {

Exception: @Model.Exception.Message
Controller: @Model.ControllerName
Action: @Model.ActionName

@Model.Exception.StackTrace
}

maxspan
3#
 I would Recommend to use Global.asax.cs File.  protected void Application_Error(Object sender, EventArgs e) { var exception = Server.GetLastError(); if (exception is HttpUnhandledException) { Server.Transfer("~/Error.aspx"); } if (exception != null) { Server.Transfer("~/Error.aspx"); } try { // This is to stop a problem where we were seeing "gibberish" in the // chrome and firefox browsers HttpApplication app = sender as HttpApplication; app.Response.Filter = null; } catch { } } 
Machinegon
4#
 I've done pablo solution and I always had the error (MVC4) The view 'Error' or its master was not found or no view engine supports the searched location. To get rid of this, remove the line  filters.Add(new HandleErrorAttribute());  in FilterConfig.cs
coderpros
5#
 I do something that requires less coding than the other solutions posted. First, in my web.config, I have the following:   And the controller (/Controllers/ErrorPageController.cs) contains the following: public class ErrorPageController : Controller { public ActionResult Oops(int id) { Response.StatusCode = id; return View(); } }  And finally, the view contains the following (stripped down for simplicity, but it can conta: @{ ViewBag.Title = "Oops! Error Encountered"; }

@Response.Status

Possible causes:

• Baptist explanation: There must be sin in your life. Everyone else opened it fine.
• Presbyterian explanation: It's not God's will for you to open this link.
• Word of Faith explanation: You lack the faith to open this link. Your negative words have prevented you from realizing this link's fulfillment.
• Charismatic explanation: Thou art loosed! Be commanded to OPEN!
• Unitarian explanation: All links are equal, so if this link doesn't work for you, feel free to experiment with other links that might bring you joy and fulfillment.
• Buddhist explanation: .........................
• Episcopalian explanation: Are you saying you have something against homosexuals?
• Christian Science explanation: There really is no link.
• Atheist explanation: The only reason you think this link exists is because you needed to invent it.
• Church counselor's explanation: And what did you feel when the link would not open?

HTTP @Response.StatusCode - @Response.StatusDescription

 It's just as simple as that. It could be easily extended to offer more detailed error info, but ELMAH handles that for me & the statusCode & statusDescription is all that I usually need.
6#
 Here is my solution. Use [ExportModelStateToTempData] / [ImportModelStateFromTempData] is uncomfortable in my opinion. ~/Views/Home/Error.cshtml: @{ ViewBag.Title = "Error"; Layout = "~/Views/Shared/_Layout.cshtml"; }

Error

@Html.ValidationMessage("Error")

 ~/Controllers/HomeController.sc: public class HomeController : BaseController { public ActionResult Index() { return View(); } public ActionResult Error() { return this.View(); } ... }  ~/Controllers/BaseController.sc: public class BaseController : Controller { public BaseController() { } protected override void OnActionExecuted(ActionExecutedContext filterContext) { if (filterContext.Result is ViewResult) { if (filterContext.Controller.TempData.ContainsKey("Error")) { var modelState = filterContext.Controller.TempData["Error"] as ModelState; filterContext.Controller.ViewData.ModelState.Merge(new ModelStateDictionary() { new KeyValuePair("Error", modelState) }); filterContext.Controller.TempData.Remove("Error"); } } if ((filterContext.Result is RedirectResult) || (filterContext.Result is RedirectToRouteResult)) { if (filterContext.Controller.ViewData.ModelState.ContainsKey("Error")) { filterContext.Controller.TempData["Error"] = filterContext.Controller.ViewData.ModelState["Error"]; } } base.OnActionExecuted(filterContext); } }  ~/Controllers/MyController.sc: public class MyController : BaseController { public ActionResult Index() { return View(); } public ActionResult Details(int id) { if (id != 5) { ModelState.AddModelError("Error", "Specified row does not exist."); return RedirectToAction("Error", "Home"); } else { return View("Specified row exists."); } } }  I wish you successful projects ;-)
DCShannon
7#
 I had everything set up, but still couldn't see proper error pages for status code 500 on our staging server, despite the fact everything worked fine on local development servers. I found this blog post from Rick Strahl that helped me. I needed to add Response.TrySkipIisCustomErrors = true; to my custom error handling code.
H. Pauwelyn
8#
H. Pauwelyn Reply to 2015-11-10 14:54:49Z
 There seem to be a number of steps here jumbled together. I'll put forward what I did from scratch. Create the ErrorPage controller public class ErrorPageController : Controller { public ActionResult Index() { return View(); } public ActionResult Oops(int id) { Response.StatusCode = id; return View(); } }  Add views for these two actions (right click -> Add View). These should appear in a folder called ErrorPage. Inside App_Start open up FilterConfig.cs and comment out the error handling filter. public static void RegisterGlobalFilters(GlobalFilterCollection filters) { // Remove this filter because we want to handle errors ourselves via the ErrorPage controller //filters.Add(new HandleErrorAttribute()); }  Inside web.config add the following  entries, under System.Web   Test (of course). Throw an unhandled exception in your code and see it go to the page with id 500, and then use a url to a page that does not exist to see 404. Thanks to everyone above. Upvoted accordingly.
user3380909
9#
 Building on the answer posted by maxspan, I've put together a minimal sample project on GitHub showing all the working parts. Basically, we just add an Application_Error method to global.asax.cs to intercept the exception and give us an opportunity to redirect (or more correctly, transfer request) to a custom error page.  protected void Application_Error(Object sender, EventArgs e) { // See http://stackoverflow.com/questions/13905164/how-to-make-custom-error-pages-work-in-asp-net-mvc-4 // for additional context on use of this technique var exception = Server.GetLastError(); if (exception != null) { // This would be a good place to log any relevant details about the exception. // Since we are going to pass exception information to our error page via querystring, // it will only be practical to issue a short message. Further detail would have to be logged somewhere. // This will invoke our error page, passing the exception message via querystring parameter // Note that we chose to use Server.TransferRequest, which is only supported in IIS 7 and above. // As an alternative, Response.Redirect could be used instead. // Server.Transfer does not work (see https://support.microsoft.com/en-us/kb/320439 ) Server.TransferRequest("~/Error?Message=" + exception.Message); } }  Error Controller: /// /// This controller exists to provide the error page /// public class ErrorController : Controller { /// /// This action represents the error page /// /// Error message to be displayed (provided via querystring parameter - a design choice) /// public ActionResult Index(string Message) { // We choose to use the ViewBag to communicate the error message to the view ViewBag.Message = Message; return View(); } }  Error page View:  Error

My Error

@ViewBag.Message

 Nothing else is involved, other than disabling/removing filters.Add(new HandleErrorAttribute()) in FilterConfig.cs public class FilterConfig { public static void RegisterGlobalFilters(GlobalFilterCollection filters) { //filters.Add(new HandleErrorAttribute()); // <== disable/remove } }  While very simple to implement, the one drawback I see in this approach is using querystring to deliver exception information to the target error page.
Robert
10#
 You can get errors working correctly without hacking global.cs, messing with HandleErrorAttribute, doing Response.TrySkipIisCustomErrors, hooking up Application_Error, or whatever: In system.web (just the usual, on/off)   and in system.webServer   Now things should behave as expected, and you can use your ErrorController to display whatever you need.
 It seems i came late to the party, but you should better check this out too. So in system.web for caching up exceptions within the application such as return HttpNotFound()   and in system.webServer for catching up errors that were caught by IIS and did not made their way to the asp.net framework   In the last one if you worry about the client response then change the responseMode="Redirect" to responseMode="File" and serve a static html file, since this one will display a friendly page with an 200 response code.
 In web.config add this under system.webserver tag as below,   and add a controller as, public class ErrorController : Controller { // // GET: /Error/ [GET("/Error/NotFound")] public ActionResult NotFound() { Response.StatusCode = 404; return View(); } [GET("/Error/ErrorPage")] public ActionResult ErrorPage() { Response.StatusCode = 500; return View(); } }  and add their respected views, this will work definitely I guess for all. This solution I found it from: Neptune Century