Question

In web.config of my ASP.NET website I have customErrors turned on and set to redirect to a specific action in case of HTTP 404:

now I start Fiddler and in my browser query some URL that yields HTTP 404. Fiddler clearly shows that first the browser queries that URL and gets HTTP 302 with the path to <...>/Error/FileNotFound and queries that and finally receives HTTP 200.

So effectively the original HTTP 404 disappears and I get HTTP 302 followed by HTTP 200.

Meanwhile if I query some invalid address from StackOverflow (like https://stackoverflow.com/ques/ask) I get HTTP 404 in a single request. I mention this because StackOverflow uses ASP.NET too.

How do I use customErrors in IIS to get HTTP 404 instead of HTTP 302 followed by HTTP 200?

Was it helpful?

Solution

In the customErrors node you can set the following attribute:

redirectMode="ResponseRewrite"

This will prevent the page from redirecting and will instead write the error page instead of the one requested.

NB: You will also have to ensure that the page being used as the 404 error page actually itself returns a 404 status code in codebehind; the MS rewrite support doesn't automatically pass the status code through, see link:

https://connect.microsoft.com/VisualStudio/feedback/details/507171/asp-net-returns-incorrect-status-code-even-with-redirectmode-responserewrite

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top