I have developed an MVC5 page and have this in web.config:
<customErrors mode="On" defaultRedirect="/Home/Error" />
the problem is that this only works when I enter a non existing file in URL, for example, "/images/nonexisting"
. However, when I enter just "/images/"
in URL (this folder does exist), an
"Error HTTP 403.14 - Forbidden"
is shown instead of custom error page.
How can I solve this?
EDIT: this is not duplicated. Referenced page is about defaultRedirect not taking into account at all. In my case, it works but only for 404 errors. For 403.14 errors, it does not work.
One other fact took my attention.
When I placed mode=Off in customErrors tag, and tried to load "/images/nonexisting", I get this error:
However, when I tried to load /images/, I got the following page:
As you see, both pages have different look. I think that is why it does not work, but I am not sure how to solve it.
It seems 404 errors are trapped by .NET Framework but 403 errors are trapped by the Web server not actually reaching the web site (that may be why custom error page is not shown). The only solution to this should be to try to configure this directly in the web Server?
Furthermore, I tried adding this to web.config, but it did not work either:
<customErrors mode="On" defaultRedirect="~/Home/Error">
<error statusCode="403" redirect="~/Home/Error" />
</customErrors>