ASP.NET Custom 404 page not returning 404 code

by Bobby Cannon July 07, 2010 08:34 PM

After trying to setup my site for Google Webmaster Tools I found that my custom ASP.NET 404 page was not returning the 404 status code. The problem lies in the fact that my custom 404 page is a content form that uses a master page. The master page will override the content form status code. It displayed the correct custom page and told the browser that everything is OK (Code 200). This is considered a soft 404 or false 404. Google doesn't like this. I found many articles on the issue but the solution I want didn't seem to work.

The solution is to add the following two lines to the code behind Render method of the custom 404 page.

   1: Response.Status = "404 Not Found";
   2: Response.StatusCode = 404;

If this doesn't work then your host provider is yet overriding your status code. The only solution at this point is to add the following code to the content. I do fill as if this is such a “hack” but I haven’t found any other way around the issue.

   1: <%
   2: // This code is required for host that do special 404 handling...
   3: Response.Status = "404 Not Found";
   4: Response.StatusCode = 404;
   5: %>

Here is my post on the question at stackoverflow.

Tags: ,

Programming

Comments

7/29/2010 9:36:21 AM #

Thanks for this post, It's great to see another BlogEngine user. Most users these days tend to use Wordpress, but in my opinion, BlogEngine is the better system to use.

HostRocket Review | Reply

7/30/2010 2:27:52 PM #

Very informative put up, love the way in which you write and I think that the information helps in a way. I don't normally say this, but I believe this is a nice job done. If you happen to like to alternate hyperlinks, I'd be more than happy to supply a hyperlink again to your site. Hope to listen to from you soon. Cheers

Webhostingpad Coupon Code | Reply

Add comment




biuquote
  • Comment
  • Preview
Loading



Month List