sTx
sTx

Reputation: 1221

403 Forbidden not redirected to ASP MVC 5 - Razor View custom <httperror> for IIS8

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:

  1. If I add 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")
  2. 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")
  3. If I remove 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")
  4. I also have a PageNotFound_404 Controller and error defined in web.config which is working fine if I write down an incorrect URL in browser.
  5. 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?

Upvotes: 0

Views: 714

Answers (2)

sTx
sTx

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

samwu
samwu

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

Related Questions