我正在尝试为我的应用程序创建自定义错误页面,它在大多数情况下都有效,但不适用于 403
错误。
我的 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>
我有一个 ErrorController
也就是委托(delegate)这些请求。当 404
点击,它会显示自定义错误页面,但是 403
才不是。我得到默认的 IIS 403 - Forbidden
页面,即使我已经为它设置了自定义错误页面。我环顾四周并尝试使用 <httpErrors>
相反,这似乎每次都给我一个空白页。
这是我的 Global.asax
如果有任何帮助:
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);
}
}
最佳答案
您可以为 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>
system.webServer 部分中的 httpErros 部分。
IIS 配置引用:
https://www.iis.net/configreference/system.webserver/httperrors
还有相关问题:
What is the difference between customErrors and httpErrors?
关于c# - ASP MVC 5 - 403 自定义错误不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42765543/