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

自定义404错误页不适用于IIS 8.5,iis,custom-errors,Iis,Custom Errors,我最近移动了主机,不得不在IIS中再次设置客户错误 我可以转到IIS管理和错误页面,如下所示: <?xml version="1.0" encoding="UTF-8"?> <configuration> <system.webServer> <httpErrors errorMode="DetailedLocalOnly" defaultResponseMode="ExecuteURL"> <

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

我可以转到IIS管理和错误页面,如下所示:

<?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>

然后,我可以转到自定义错误,并按如下方式设置选项:

<?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>

这将创建我的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没有重定向并返回标准的IIS404错误。我已经确认404错误页面在服务器上的正确位置

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

最后使用以下方法使它工作起来(通过找到此:)帮助:


我遇到了一个类似的问题,我在/Error/Missing处有一个自定义404页面,但对于不存在的静态文件或确实存在的文件夹/目录(但不应由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" />

将其更改为“替换”将再次显示默认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>


我把所有这些细节都放在这里,希望其他人也能找到我做过的事情,希望能有所帮助

对于我来说,我必须添加existingResponse=“Replace”,例如,我有这个:。不起作用。我在本地运行它,当输入错误的URL时,我不会被重定向到ErrorPages/PageNotFound.aspx”。你知道吗?