troy on June 15, 2011 04:53 sez:
This really is too hard.
troy on June 15, 2011 04:53 sez:
This really is too hard.
lb on July 25, 2011 00:02 sez:
The other thing to do, in your errorcontroller is to add this:
Response.TrySkipIisCustomErrors = true;
That way asp.net will handle the error rather than iis taking over.
Alan Stevens on September 25, 2011 19:47 sez:
Leon, have you seen this?
http://nuget.org/List/Packages/MagicalUnicornMvcErrorToolkit
Are you to blame?
++Alan
Bilal on November 17, 2011 13:22 sez:
Complete worthless!
I tried to implement all of above and got compile error, i corrected them and then tried to access non existing pages or product id's and got the usual yellow asp.net error page, that is none of the custom error pages where shown!!!
Please before putting up and article like this. Try to implement it and see whether it is working. Otherwise you are just wasting our time.
Sumit on June 05, 2012 01:44 sez:
I think the Catch-All route should be referring to 'ErrorController' instead of 'StaticContentController' so it should read
routes.MapRoute(
"404-PageNotFound",
"{*url}",
new { controller = "Error", action = "NotFound" }
);
?
Nice summary though!
Will S. on August 13, 2012 15:35 sez:
@lb: That won't work if you have validateIntegratedModeConfiguration set to "false", like I do. Kind of need that for my web sites for my web handlers.
Will S. on August 13, 2012 15:41 sez:
I meant "my web site's web handlers".
Robert on December 20, 2012 12:43 sez:
Dude, you do not know how to use ASP.NET.
You are not meant to use a redirect inside <customErrors> of the web.config and have a handle error attribute in the RegisterGlobals at the same time.
These are two seperate implementations of a similar thing.
The reason you had such a hard time getting it to work is because you had to write a lot of extra code to make them stop fighting with each other.
A custom error handler is as simple as a change to the web.config:
<customErrors mode="On">
<error statusCode="404" redirect="/Error/404" />
</customErrors>
(and any other status you want to handle)
Then, Make sure that your globals does NOT add a filter for HandleErrorAttribute under RegisterGlobalFilters.
Now your redirect is done.
If you want to try to capture extra info, you can do that as you were inside Application_Error in the globals file.
OR... you can not include the redirect in the Web.config, and you can include the filter, and write the Error controller.
There is no need to do both.
guillegr123 on May 14, 2013 19:37 sez:
Seems to be all the ways one can implement custom errors in ASP.NET MVC... But I think you cannot implement all the solutions at the same time :S
David on October 15, 2013 20:54 sez:
It took me three reads to be sure, but the author was not attempting to tell us how to do it. He was telling us how to really mess up our system by following all the confused advice, and said so. I personally just had some difficulty reading the tenor of his snark (was he really saying it would bork your system, or was he being ironic?).
Anyway, Roboert, while missing the snark, did provide an answer that works for real. Thanks Robert!
For the moment, the config capture of non-500 errors, combined with the default error handling attribute seems to work. I get to my custom controller error view for the 404 (etc.), but I go to the MVC Shared/Error.cshtml for thrown exceptions. Combined with ELMAH.MVC, I think I'm on the road to an effective error handling design. I might remove the filter and go completely custom, or I might not. Time will tell.
lb on October 15, 2013 21:06 sez:
@David
Yeh, it is all meant as snark. Just re-reading it now I am amazed by the whole thing. I'm not usually this psychotic, handing out such a deviously crafted piece of confusion.
Robert's answer is great, because he works through his frustration and gives a nice simple solution.
JV on June 20, 2014 07:57 sez:
Your code is written under an assumption that the site will be deployed as a root web application in IIS.
To be a generic solution, you'll need to make use of "~" appropriately in your paths.