Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

Having a weird problem here. Everybody knows that if you use web.config's customErrors section to make a custom error page, that you should set your Response.StatusCode to whatever is appropriate. For example, if I make a custom 404 page and name it 404.aspx, I could put <% Response.StatusCode = 404 %> in the contents in order to make it have a true 404 status header.

Follow me so far? Good. Now try to do this on IIS7. I cannot get it to work, period. If Response.StatusCode is set in the custom error page, IIS7 seems to override the custom error page completely, and shows it's own status page (if you have one configured.)

Has anyone else seen this behavior and also maybe know how to work around it? It was working under IIS6, so I don't know why things changed.

Update: This is not the same as the issue in http://stackoverflow.com/questions/347281/asp-net-custom-404-returning-200-ok-instead-of-404-not-found

share|improve this question
Bobby, I actually found that question and tried it, but it didn't fix the problem. But thanks. – Nicholas Jan 12 '09 at 3:37

4 Answers

up vote 35 down vote accepted

The easiest way to make the behavior consistent is to use Response.TrySkipIisCustomErrors and set it to true. This will override the IIS global error page handling from within your page.

More info here: http://www.west-wind.com/weblog/posts/745738.aspx

share|improve this answer
11  
Didn't work for me. IIS7 configuration is pure black magic. – Mauricio Scheffer Oct 14 '11 at 14:34

Set existingResponse to PassThrough in system.webServer/httpErrors section:

  <system.webServer>
    <httpErrors existingResponse="PassThrough" />
  </system.webServer>

Default value of existingResponse property is Auto:

Auto tells custom error module to do the right thing. Actual error text seen by clients will be affected depending on value of fTrySkipCustomErrors returned in IHttpResponse::GetStatus call. When fTrySkipCustomErrors is set to true, custom error module will let the response pass through but if it is set to false, custom errors module replaces text with its own text.

More information: What to expect from IIS7 custom error module

share|improve this answer
Thank you! This was exactly what I needed! – Nathan Taylor Sep 16 '09 at 2:37
Note that setting existingResponse to PassThrough can lead to some side effect. Please master the link provided by Pavel before any changes. – Lex Li Feb 12 '10 at 8:09
Is <httpErrors existingResponse="PassThrough" /> equivalent to Response.TrySkipIisCustomErrors or do they behave differently? – asbjornu Jun 30 '11 at 9:12
2  
You saved my free night! :) – reinder Nov 30 '11 at 14:53
1  
oh my god, this works much better than the damn Response.TrySkipIisCustomErrors, still wonder why the latter doesn't work – uowzd01 Jun 29 '12 at 7:24
show 6 more comments

Solved: It turns out that "Detailed Errors" needs to be on in order for IIS7 to "passthrough" any error page you might have. See http://forums.iis.net/t/1146653.aspx

share|improve this answer
1  
Though this one was marked as the answer, I think it worths the while to read other replies to gain more insights on the topic. – Lex Li Feb 12 '10 at 8:12

By default IIS 7 uses detailed custom error messages so I would assume that Response.StatusCode will equal 404.XX rather than just 404.

You can configure IIS7 to use the simpler error message codes or modify your code handling the more detailed error messages that IIS7 offers.

More info available here: http://blogs.iis.net/rakkimk/archive/2008/10/03/iis7-enabling-custom-error-pages.aspx

Further investigation revealed I had it the wrong way around - detailed messages aren't by default but perhaps they've been turned on, on your box if you're seeing the different error messages that you've mentioned.

share|improve this answer
Response.StatusCode is an integer, so I don't see a way of setting a more specific code than just "404". I've got IIS7 configured to use/show custom error pages, like your URL indicates. – Nicholas Jan 12 '09 at 3:45
Hmmm...Unfortunately I can't test at the moment as I'm not on my home pc. If you don't have a solution by then - I will have a look tonight. – nullnvoid Jan 12 '09 at 3:49
Thanks, the issue is driving me crazy. I can't believe no one else has run across this? – Nicholas Jan 12 '09 at 9:27
Glad you found the solution - I just logged in to do some research and you've already solved it! Cheers! – nullnvoid Jan 12 '09 at 12:32

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.