我的web.config:
<customErrors mode="Off" redirectMode="ResponseRewrite" defaultRedirect="~/Pages/Error/DefaultError.aspx">
<error statusCode="404" redirect="~/Pages/Error/Page404.aspx" />
<error statusCode="500" redirect="~/Pages/Error/DefaultError.aspx" />
</customErrors>
<httpErrors existingResponse="Replace" defaultResponseMode="ExecuteURL" errorMode="Custom"> //also used other options for existingResponse
<remove statusCode="403"/>
<error statusCode="403" path="~/Pages/Error/PI403.aspx" responseMode="ExecuteURL"/>
</httpErrors>
这是我在global.asax.cs文件中的Application_error方法:
Server.ClearError(); //clear the error so we can continue onwards
var errorPage = "~/Pages/Error/DefaultError.aspx";
var httpException = ex as HttpException;
if (httpException != null && httpException.GetHttpCode() == (int)HttpStatusCode.NotFound)
{
errorPage = "~/Pages/Error/Page404.aspx";
}
Response.Redirect(errorPage);
我在项目中有Logs文件,但是此文件仅使用日志记录。如果使用myURL / Logs浏览器链接,则会出现IIS 403.14错误页面。如果编写myURL / asdeds,则会得到自定义错误页面(在我的项目中不存在类似的内容)。因为403异常不会触发Application_Error。我想显示所有异常的自定义错误页面。当我将myURL / Logs写入URL部分时,应该会看到我的自定义错误页面。
并且我还在Application_BeginRequest中设置了TrySkipIisCustomError属性
HttpContext.Current.Response.TrySkipIisCustomErrors = true;
答案 0 :(得分:0)
由于403.14-禁止的错误是IIS错误而不是asp.net错误。它使用StaticFile http处理程序而不是asp.net http处理程序。因此,当您遇到此错误时,不会触发Application_error。
修改自定义错误页面的唯一方法是使用IIS自定义错误页面。
正如Lex所说,IIS自定义错误页面不支持“〜”。它将自动匹配您的Web应用程序的根路径。因此,您可以使用以下配置设置。
<httpErrors existingResponse="Replace" defaultResponseMode="ExecuteURL" errorMode="Custom"> //also used other options for existingResponse
<remove statusCode="403"/>
<error statusCode="403" path="/Pages/Error/PI403.aspx" responseMode="ExecuteURL"/>
</httpErrors>
还可以打开IIS管理控制台并使用UI窗口修改设置。
答案 1 :(得分:0)