Custom 404 error page not working on IIS 8.5

做~自己de王妃 提交于 2019-12-01 03:57:42

Got it working in the end (helped by finding this: http://forums.iis.net/t/1173965.aspx), using:

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

I was having a similar problem where I have a custom 404 page at /Error/Missing but it wasn't showing up for static files that didn't exist or for folders/directories that DID exist (but shouldn't be served by MVC). The controller for Missing page has the following:

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

Also I wasn't getting my custom error page if I returned the following in a controller:

return HttpNotFound();

I could change the IIS default errors to a blank page if I set PassThrough:

<httpErrors existingResponse="PassThrough" />

Changing it to "Replace" made the default IIS errors show again.

I also had a section in my web.config, but I've taken it out as I'm IIS 8.5 it doesn't look like it's needed any more.

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

So basically I couldn't get rid of default IIS messages - either one-liner or the more detailed ones. My httpErrors section looked like this:

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

Finally I came across this question and I was looking at the other answer on this question and realised that I could try a ResponseMode on each error line. I thought that wouldn't be necessary as I had the defaultResponseMode set - but it makes a difference!!

So, if you want to serve up a custom 404 page use this httpErrors module:

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

I've put all these details here so this hopefully shows up for someone else searching the same things I did - hope it helps!

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!