I need to have a custom error page to handle all http errors. When an http error happens, page should show the custom page, and it's content must be generated dynamically based on the http error status code. I half managed to show a custom html when a specific http error occurs. For example 404. But there's two problems. 1. I need to redirect to a .cshtml, so I can do some processing on the response and create the content accordingly to the http error status code. 2. I need a method to redirect all errors to this .cshtml file.
I've read docs and searched through similar questions in SO, followed them all and no progress.
Currently I have the following lines in web.config
:
<httpErrors errorMode="Custom" defaultResponseMode="File" defaultPath="CustomError.htm">
<clear />
<error path="CustomError.htm" responseMode="File" statusCode="403"/>
</httpErrors>
This will show the content of the file CustomError.htm
which is located in the same path of the file web.config
.
But as I read the docs I expected this config to work:
<httpErrors errorMode="Custom" defaultResponseMode="File" defaultPath="CustomError.htm">
<clear />
</httpErrors>
But it does not show the contents of CustomError.htm file, instead it shows a blank page with the following sentence:
You do not have permission to view this directory or page.
Also even for handling a single httpError, I have trouble using a .cshtml and I had to use a html file.
I actually have the following cshtml file: Views/Error/PageNotFound.cshtml
It has a controller and an actionResult method called: PageNotFound
Now, when I do this:
<httpErrors errorMode="Custom" defaultResponseMode="File" defaultPath="CustomError.htm">
<clear />
<error path="~/Error/PageNotFound" responseMode="ExecuteURL" statusCode="404"/>
</httpErrors>
Then, the browser throws a not found error.
When I change responseMode
to Redirect
, then the path gets appended to the existing url over and over and won't work.
As you can see in the tags, I'm using asp.net mvc and IIS 10 on windows 10.
So any help?