问题
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