Reputation: 1221
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:
subStatusCode
with 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")subStatusCode
: <error statusCode="403" path="~/CustomErr/Forbidden_403" responseMode="ExecuteURL" />
then the browser shows a HTTP 404 error("The webpage cannot be found")subStatusCode
and 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")PageNotFound_404
Controller and error defined in web.config
which is working fine if I write down an incorrect URL in browser.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?
Upvotes: 0
Views: 714
Reputation: 1221
What I eventually did was to override paths from Web.config
in Web.Release.config
<httpErrors errorMode="Custom" existingResponse="Replace">
<remove statusCode="403" />
<remove statusCode="404" />
<error statusCode="403" path="/myAppFolder/CustomErr/Forbidden_403" responseMode="ExecuteURL" />
<error statusCode="404" path="/myAppFolder/CustomErr/NotFound_404" responseMode="ExecuteURL" />
</httpErrors>
Also all js files with ajax that have url to controllers, I've modified urls from url:"/Controller/Action"
to url:"Controller/Action"
- they will automatically figure relative path.
Upvotes: 1
Reputation: 5205
You should remove subStatusCode and relative path("~"), and shouldn't the end of the value of your path
be html or htm? below code works on my side.
<httpErrors errorMode="Custom" existingResponse="Replace">
<remove statusCode="404"/>
<error statusCode="404" path="/Errors/404.html" responseMode="ExecuteURL"/>
</httpErrors>
Upvotes: 0