TheMethod
TheMethod

Reputation: 3000

Setting 404 response while still displaying page

I have a details page for products. The product is fetched by id in a query string, if a product with the id is found in the database we display the details, otherwise we show a "this item can not be found" message. Pretty standard stuff. What I would like to do is show the details page with the "this item can not be found' message but send a 404 response. This is so that Google will un-index items that have been removed.

So I have something like this(simplified):

<asp:Panel ID="pnlDetails" runat="server" Visible="false">
    item details go here
</asp:Panel>

<asp:Panel ID="pnlError" runat="server" Visible="false">
    <p>The specified  item could not be found.</p>
</asp:Panel>

And in the code behind:

if(itemFound)
{
   showDetails();
}
else
{
   showError();
}

private void showDetails()
{
   pnlDetails.Visible = true; 
   //fill in details
}

private void showError()
{
    //set response
    Response.StatusCode = 404;
    pnlError.Visible = true;
}

What is happening now is I see the error panel but I still get a 200 response. Could anyone tell me what I am doing wrong? Any advice would be appreciated, thanks much!

Edit: This I am calling these methods in the Page_Load event handler.

Upvotes: 0

Views: 3144

Answers (3)

puddleglum
puddleglum

Reputation: 1045

I see... since your code won't throw a 404, you infact want it too so google will clean up your dead links naturally.... try this: stackoverflow.throwing404errorsForMissingParameters

also, this is helpful (near the bottom) forums.asp.net/throwing404InHTTPResponse. For example HttpContext.Current.Respone.StatusCode = 404;

protected void Page_Load(object sender, EventArgs e) 
{ 
    Response.StatusCode = 404; 
    Response.End(); 
}

Upvotes: 2

Mike Perrenoud
Mike Perrenoud

Reputation: 67948

Set the status code in the Render method, so it might look something like this:

protected override void Render(HtmlTextWriter writer) 
{ 
    base.Render(writer); 
    Response.StatusCode = 404; 
    pnlError.Visible = true;
} 

Upvotes: 1

Saurabh R S
Saurabh R S

Reputation: 3177

Google says that..

If outdated pages from your site appear in the search results, ensure that the pages return a status of either 404 (not found) or 410 (gone) in the header.

Here is the source
So what you can do..

  1. Incase the item is not found - Redirect to your custom 404 page.
  2. In the Page_Load event of your 404 page add this..

    Response.StatusCode = 404;

But just by doing this it return HTTP 302 Redirect code and then HTTP 200 - ok code. So the page will not be removed from Google's indexes.
3. Open the Global.asax file (if not present, add it). Add the following code to handle 404 (Page not found) errors

     protected void Application_Error(object sender, EventArgs e)
{
    Exception ex = Server.GetLastError();
    if (ex is HttpException)
    {
        if (((HttpException)(ex)).GetHttpCode() == 404)
            Server.Transfer("~/404.aspx");
    }
    Server.Transfer("~/AnyOtherError.aspx");
}

However in this case make sure you have no customErrors configuration in web.config for 404. Hope it helps.

Upvotes: 1

Related Questions