I've been fighting to get custom error pages to work and keep finding my way back to simply using a static page. Though the static page works, it would require remaking the navigation bar which we would like to avoid at this time. I'm currently using the following to specify custom error pages.
Asp.net Error Handling
<customErrors mode="On" redirectMode="ResponseRewrite">
<error statusCode="404" redirect="~/404.aspx"/>
</customErrors>
IIS Error Handling
<httpErrors errorMode="Custom">
<remove statusCode="404"/>
<error statusCode="404" path="/404.html" responseMode="File"/>
</httpErrors>
Is there a method of implementing dynamic custom error pages that can handle for both IIS errors and Asp.net errors?
I struggled with this as well and searched for a long time. As far as I can tell there is no way to create a dynamic custom error page that gets served up for IIS Errors for requests that don't enter the .net pipeline. Like you, I ended up having two 404 error pages. One dynamic .aspx one for file not founds errors resulting from requests that entered the .net pipeline, and one .html one for file not found requests that never entered the .net pipeline. The person who downvoted your question probably didn't realize that you are asking a very good, and difficult question, that has no easy solution. I will upvote your question to help compensate.
I got around the same issue in my ASP.Net MVC projects by piping everything through .Net via handlers.
First an
ErrorController
was created to handle request errors and not found requests like.You will notice that I call
TrySkipIisCustomErrors
to try to avoid IIS errorsThen a base controller to handle all unknown actions that would map to the
ErrorController.NotFound
action was created.All
Controllers
would inherit from this base controller.A catch all route was configured after all other routes.
This made sure that if a route was not matched to any of my controllers it would safely route to the
ErrorController.NotFound
action.For the views, I created the respective
NotFound.shtml
andError.cshtml
paged in theViews/Shared
folder and they benefited from access to the root layout, which is what I think you were looking for.In the end I was able to remove both the
customErrors
andhttpErrors
fromweb.config
as there was no longer any need for them as every request was managed by the handlers and routed accordingly.The original idea for this structure came from this article where I mixed and matched the available options till I found a solution that worked/suited my needs.
Exception handling in ASP.NET MVC (6 methods explained)
Hope this helps.