Custom 404 error page not working on IIS 8.5

后端 未结 2 1539
生来不讨喜
生来不讨喜 2020-12-19 02:50

I have recently moved host and have had to set up Customer Errors again in IIS.

I can go to IIS Admin and Error Pages as follows:

相关标签:
2条回答
  • 2020-12-19 03:06

    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>
    
    0 讨论(0)
  • 2020-12-19 03:32

    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!

    0 讨论(0)
提交回复
热议问题