IIS7 custom 404 not showing
Asked Answered
R

4

42

created a new IIS7 web site with Intergrated .net 4.0 app pool.

URLs ending with .aspx do show custom 404 anything else gives the blue server error page "HTTP Error 404.0 - Not Found The resource you are looking for has been removed, had its name changed, or is temporarily unavailable." (so nothing to do with IE)

<customErrors redirectMode="ResponseRewrite" mode="On" defaultRedirect="/pages/404.aspx" />
</system.web>
<system.webServer>
    <httpErrors  >
        <remove statusCode="404" subStatusCode="-1" />
        <error statusCode="404" prefixLanguageFilePath="" path="/pages/404.aspx" responseMode="ExecuteURL" />
    </httpErrors>
</system.webServer>

also tried

<httpErrors existingResponse="PassThrough" />

but that just resulted in an empty response.

I have only found one reference to the usefulness of executing the appcmd to test the custom http error handling but here are the results.

C:\Windows\System32\inetsrv>appcmd list config "http://mysite/file.notexist" -section:httpErrors

<system.webServer>
    <httpErrors>
        <error statusCode="401" prefixLanguageFilePath="%SystemDrive%\inetpub\custer
r" path="401.htm" />
        <error statusCode="403" prefixLanguageFilePath="%SystemDrive%\inetpub\custer
r" path="403.htm" />
        <error statusCode="404" prefixLanguageFilePath="%SystemDrive%\inetpub\custer
r" path="404.htm" />
        <error statusCode="405" prefixLanguageFilePath="%SystemDrive%\inetpub\custer
r" path="405.htm" />
        <error statusCode="406" prefixLanguageFilePath="%SystemDrive%\inetpub\custer
r" path="406.htm" />
        <error statusCode="412" prefixLanguageFilePath="%SystemDrive%\inetpub\custer
r" path="412.htm" />
        <error statusCode="500" prefixLanguageFilePath="%SystemDrive%\inetpub\custer
r" path="500.htm" />
        <error statusCode="501" prefixLanguageFilePath="%SystemDrive%\inetpub\custer
r" path="501.htm" />
        <error statusCode="502" prefixLanguageFilePath="%SystemDrive%\inetpub\custer
r" path="502.htm" />
    </httpErrors>
</system.webServer>

which is odd because in iis7 manager the error pages show

404 /pages/404.aspx Execute URL Local

.Net Error Pages shows nothing, though I did have an entry in there.

Question 1 : what steps do I need to take for a completely new asp .net 4 iis7 site to have a custom .net error page for every 404 result ?

Question 2 : why does the .net handler work for .aspx files and nothing else ?

note: set the 404 at server level and the appcmd command then showed the custom 404 in the path, but made no difference to the site failing to showing 404.

So I am guessing it's a red herring.

Rectangular answered 11/7, 2011 at 9:14 Comment(0)
R
117

answer was to use

    <httpErrors existingResponse="Replace" errorMode="Custom">
        <remove statusCode="404" subStatusCode="-1" />
        <error statusCode="404" prefixLanguageFilePath="" path="/pages/404.aspx?he" responseMode="ExecuteURL" />
    </httpErrors>

and not to have any system.web customErrors

this worked for both .aspx and non .aspx requests.

bizarrely this combination did not come up in any of the blog posts and stackoverflow answers I had investigated, it was just luck I tried it.

Rectangular answered 12/7, 2011 at 8:57 Comment(9)
Thanks for posting this answer - this just made me look like I knew what I was doing! :)Lithometeor
Wish I could upvote this a couple extra times. Thanks for the help!...Also worth mentioning that I noticed from this post, is that the "path" section should NOT start with a "~"Gynecology
Thanks - when setting this through IIS is does not set the attributes on the httpError element. This is were MicroSoft fails! Why would they put it in and not document it?Baugher
If you have Custom Errors On then use @mhenry1384 's answerBahamas
Do you mind posting all the web.config because it helps to see all that needs to get done without us who are new to the question having to jump around for hours piecing things together. Thanks.Blackjack
IIS / ASP.NET custom error pages are horrendously complex.Tristis
The key item that fixed my issue was to add the existingResponse="Replace" errorMode="Custom" properties to the httpErrors node.Hagerty
I came from this answer and was driving me mad!!!, your solution worked just fine.Ordonez
I needed a way to be able to show custom JSON responses, I have jumped some serious hurdles trying to get something to work. The root web.Config change is a no-go for my requirement. This solution is absolutely spot on, I cannot thank you enough!Helfant
R
6
<httpErrors existingResponse="PassThrough" />

worked for me in IIS 7 (Windows Server 2008 R2).

My problem was I had this in my web.config, but in a <location>. Moving it to the root <system.webServer> fixed it.

<?xml version="1.0" encoding="utf-8"?>
<configuration>
    ...
    <system.webServer>
        ...
        <httpErrors existingResponse="PassThrough" />
    </system.webServer>
</configuration>
Rafaelof answered 8/10, 2013 at 2:40 Comment(3)
Could you to post the entire web.config so we can see what you are talking about?Blackjack
Ah, sorry. I'd like to help but I don't do .NET anymore and don't have access to my old code.Rafaelof
Thank you for putting the httpErrors element in context of the web.config so we know where to put it.Heine
M
0

For IIS7 onwards, go with httpErrors only, as per rob's answer: https://stackoverflow.com/a/6661699

Just to add, unless you need/want to use ASP.NET to render your error page, I would recommend using static HTML files to remove the dependency on ASP.NET. Just be sure to omit any leading forward slash and use backslashes for the rest of the path, e.g.

<error statusCode="404" prefixLanguageFilePath="" path="pages\404.html" responseMode="File" />

Set responseMode="File" to retain the correct status code.

Mahone answered 26/7, 2017 at 10:7 Comment(2)
this could've been a comment on original answer.Burnedout
In hindsight, you're probably right. At the time I felt my point was significant enough to warrant a new answer.Mahone
I
0

I was having this issue with my web app and I got it resolved by commenting out the redirect line:

    <httpErrors>
        <remove statusCode="403" subStatusCode="-1" />
        <!--<error statusCode="403" prefixLanguageFilePath="" path="C:\inetpub\redirectSItoHttps.htm" responseMode="File" />-->
    </httpErrors>

On IIS, the allowAbsolutePathsWhenDelegated (on Configuration Editor --> system.webServer/httpErrors) was locked and I can't change the value false to true.

Intercalate answered 16/8, 2018 at 13:1 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.