tags:

views:

3525

answers:

5

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

A: 

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.

nullnvoid
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 H
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
Thanks, the issue is driving me crazy. I can't believe no one else has run across this?
Nicholas H
Glad you found the solution - I just logged in to do some research and you've already solved it! Cheers!
nullnvoid
+1  A: 

I had the same question.

Already answered here http://stackoverflow.com/questions/347281/asp-net-custom-404-returning-200-ok-instead-of-404-not-found.

Bobby Cannon
Bobby, I actually found that question and tried it, but it didn't fix the problem. But thanks.
Nicholas H
+3  A: 

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

Nicholas H
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
+6  A: 

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

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

More information: What to expect from IIS7 custom error module

Pavel Chuchuva
Thank you! This was exactly what I needed!
Nathan Taylor
Note that setting existingResponse to PassThrough can lead to some side effect. Please master the link provided by Pavel before any changes.
Lex Li
+7  A: 

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

Rick Strahl