简体   繁体   中英

ASP MVC 5 - 403 customError not working

I am trying to create custom error pages for my application and it's working for the most part, but not for 403 errors.

My Web.config:

<customErrors mode="On" defaultRedirect="~/Error">
  <error statusCode="404" redirect="~/Error/NotFound" />
  <error statusCode="500" redirect="~Error/InternalServer" />
  <error statusCode="403" redirect="~Error/Forbidden" />
</customErrors>

I have an ErrorController that is delegating these requests. When the 404 hits, it displays the custom error page, but 403 does not. I am getting the default IIS 403 - Forbidden page even though I have set a custom error page for it. I've had a look around and tried to use <httpErrors> instead which just seems to give me a blank page everytime.

Here is my Global.asax if it's any help:

void Application_Error(object sender, EventArgs e)
{
    Exception exc = Server.GetLastError();

    if (exc is HttpUnhandledException)
    {
        // Pass the error on to the error page.
        Server.Transfer("ErrorPage.aspx?handler=Application_Error%20-%20Global.asax", true);
    }
}

U can use new approach for IIS 7+.

<httpErrors errorMode="Custom" existingResponse="Replace">
      <remove statusCode="403" />
      <remove statusCode="404" />
      <remove statusCode="500" />
      <error statusCode="403" path="/Error" responseMode="ExecuteURL" />
      <error statusCode="404" path="/Error/404" responseMode="ExecuteURL" />
      <error statusCode="500" path="/Error/500" responseMode="ExecuteURL" />
</httpErrors>

httpErros section in system.webServer section.

IIS configuration refence: https://www.iis.net/configreference/system.webserver/httperrors
And also related question: What is the difference between customErrors and httpErrors?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM