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:
<customErrors mode="On" defaultRedirect="~/Views/Shared/Error.cshtml"> <error statusCode="403" redirect="~/Views/Shared/UnauthorizedAccess.cshtml" /> <error statusCode="404" redirect="~/Views/Shared/FileNotFound.cshtml" /> </customErrors>
Registered
HandleErrorAttribute
as a global action filter in theFilterConfig
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?
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.In web.config add this under system.webserver tag as below,
and add a controller as,
and add their respected views, this will work definitely I guess for all.
This solution I found it from: Neptune Century
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.Error Controller:
Error page View:
Nothing else is involved, other than disabling/removing
filters.Add(new HandleErrorAttribute())
in FilterConfig.csWhile very simple to implement, the one drawback I see in this approach is using querystring to deliver exception information to the target error page.
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:
And finally, the view contains the following (stripped down for simplicity, but it can conta:
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.
Here is my solution. Use [ExportModelStateToTempData] / [ImportModelStateFromTempData] is uncomfortable in my opinion.
~/Views/Home/Error.cshtml:
~/Controllers/HomeController.sc:
~/Controllers/BaseController.sc:
~/Controllers/MyController.sc:
I wish you successful projects ;-)
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.