I try to redirect users that have no autorization for different pages from my app to a custom error page: CustomErr/Forbidden_403 - controller(that have a razorview). I've also configured <customErrors> in my Web.config but I need <httpErrors> too because for some reason, when I publish to the server, this 403 error is not overriden(more it shows 404 not found error instead of 403).
I need a relative path to the View: path="~/CustomErr/Forbidden_403" because my server have multiple apps and each app have it's own folder.
What I've seen so far:
- If I add
subStatusCodewith or without relative path("~") :<error statusCode="403" subStatusCode="14" path="~/CustomErr/Forbidden_403" responseMode="ExecuteURL" />then the browser return a HTTP 403 error page ("The website declined to show this webpage") - If I remove
subStatusCode:<error statusCode="403" path="~/CustomErr/Forbidden_403" responseMode="ExecuteURL" />then the browser shows a HTTP 404 error("The webpage cannot be found") - If I remove
subStatusCodeand relative path("~"):<error statusCode="403" path="/CustomErr/Forbidden_403" responseMode="ExecuteURL" />then the browser return a HTTP 403 error page ("The website declined to show this webpage") - I also have a
PageNotFound_404Controller and error defined inweb.configwhich is working fine if I write down an incorrect URL in browser. - The custom error is not showing only for 403 Forbidden => I user return
new HttpStatusCodeCresult(HttpStatusCOde.Forbidden)for throwing this 403 code to server.
Web.config
<httpErrors errorMode="Custom" existingResponse="Replace">
<remove statusCode="403" />
<remove statusCode="404" />
<error statusCode="403" path="~/CustomErr/Forbidden_403" responseMode="ExecuteURL" />
<error statusCode="404" path="~/CustomErr/NotFound_404" responseMode="ExecuteURL" />
</httpErrors>
Do I need to configure it from IIS?
You should remove subStatusCode and relative path("~"), and shouldn't the end of the value of your
pathbe html or htm? below code works on my side.