views:

39

answers:

2

In the web.config you can add this code to handle 404 errors:

<customErrors mode="Off" defaultRedirect="ErrorPage.aspx">
  <error statusCode="404" redirect="/Site-Map"/>
</customErrors>

And this works well as you can see by this URL http://www.fundraising.com.au/thisisnotapage

However, according to this website http://gsitecrawler.com/tools/Server-Status.aspx the response code for this non-existent page is 200, not a 404.

Whilst this page http://www.nuenergy.com.au/thisisnotapage does show a 404 at http://gsitecrawler.com/tools/Server-Status.aspx and redirects to another page.

How do you achieve the later in ASP.NET? I need the response to be a 404 and for another page to show up.

+1  A: 

The custom errors section employs the HttpHandler to intercept the fact you don't have a page and then redirects the response so that the server returns Site-Map instead which is an available resource so the 200 status is correct since its as though the request was for Site-Map in the first place.

You ultimately want to serve two versions of Site-Map, one when it is the actual request (200) but also one when it is the custom error response (404). I would create another version of the Site-Map page, OnError-Site-Map, extending the pages class to inherit it's features but then overriding the http response code so that it delivers the 404 status you want but for that extended version only. You then put that in the custom errors section in the web.config instead.

Dave Anderson
+1  A: 

For your page to return a 404 status, add the following code to your Page_Load event:

Protected Sub Page_Load1(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

    Response.Status = "404 Not Found"

End Sub
NightOwl888