自定义 404 错误页面不适用于 IIS 8.5

Custom 404 error page not working on IIS 8.5

我最近移动了主机,不得不在 IIS 中重新设置客户错误。

我可以按如下方式转到 IIS 管理和错误页面:

然后我可以进入自定义错误,并设置如下选项:

创建我的 web.config 文件如下:

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <system.webServer>
        <httpErrors errorMode="DetailedLocalOnly" defaultResponseMode="ExecuteURL">
            <remove statusCode="500" subStatusCode="100" />
            <remove statusCode="500" subStatusCode="-1" />
            <remove statusCode="404" subStatusCode="-1" />
            <error statusCode="404" prefixLanguageFilePath="" path="/error_404.asp" responseMode="ExecuteURL" />
            <error statusCode="500" prefixLanguageFilePath="" path="/error_500.asp" responseMode="ExecuteURL" />
            <error statusCode="500" subStatusCode="100" path="/error_500.asp" responseMode="ExecuteURL" />
        </httpErrors>
    </system.webServer>
</configuration>

当我测试页面时,505 错误正常工作,并重定向到正确的页面,但 404 不重定向并且 returns 标准 IIS 404 错误。我已确认 404 错误页面位于服务器上正确的位置。

我看不出我还需要做什么。

最后成功了(通过找到这个帮助:http://forums.iis.net/t/1173965.aspx),使用:

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <system.webServer>
        <httpErrors errorMode="Custom" defaultResponseMode="ExecuteURL">
            <remove statusCode="500" subStatusCode="100" />
            <remove statusCode="500" subStatusCode="-1" />
            <remove statusCode="404" subStatusCode="-1" />
            <error statusCode="404" path="/error_404.asp" responseMode="ExecuteURL" />
            <error statusCode="500" prefixLanguageFilePath="" path="/error_500.asp" responseMode="ExecuteURL" />
            <error statusCode="500" subStatusCode="100" path="/error_500.asp" responseMode="ExecuteURL" />
        </httpErrors>
    </system.webServer>
</configuration>

我遇到了类似的问题,我在 /Error/Missing 有一个自定义 404 页面,但它没有显示不存在的静态文件或 folders/directories DID 存在(但不应由 MVC 提供)。缺少页面的控制器具有以下内容:

    Response.AddHeader("Content-Type","text/html; charset=utf-8");
    Response.TrySkipIisCustomErrors = true;
    Response.StatusCode = (int)HttpStatusCode.NotFound; // 404

此外,如果我在控制器中返回以下内容,我也不会收到自定义错误页面:

return HttpNotFound();

如果我设置 PassThrough:

,我可以将 IIS 默认错误更改为空白页
<httpErrors existingResponse="PassThrough" />

将其更改为 "Replace" 使默认的 IIS 错误再次显示。

我的 web.config 中也有一个部分,但我已将其删除,因为我是 IIS 8.5,看起来不再需要它了。

<system.web>
    <customErrors mode="Off">
</system.web>

所以基本上我无法摆脱默认的 IIS 消息 - 单行消息或更详细的消息。我的 httpErrors 部分如下所示:

<httpErrors errorMode="Custom" defaultResponseMode="ExecuteURL">
    <remove statusCode="404" />
    <error statusCode="404" path="/Error/Missing" />
</httpErrors>

最后我遇到了这个问题,我正在查看关于这个问题的其他答案并意识到我可以在每个错误行上尝试 ResponseMode。我认为这不是必需的,因为我设置了 defaultResponseMode - 但它有所不同!!

因此,如果您想提供自定义 404 页面,请使用此 httpErrors 模块:

<httpErrors errorMode="Custom">
    <remove statusCode="404" />
    <error statusCode="404" path="/Error/Missing" responseMode="ExecuteURL" />
</httpErrors>

我已将所有这些详细信息放在这里,希望这对搜索我所做的相同内容的其他人有所帮助 - 希望对您有所帮助!