ASP MVC 5 - 403 customError not working

旧时模样 提交于 2021-01-27 13:18:22

问题


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);
    }
}

回答1:


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?



来源:https://stackoverflow.com/questions/42765543/asp-mvc-5-403-customerror-not-working

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!