Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Manually throw 404 exception with correct status code

Status
Not open for further replies.

jgd1234567

Programmer
May 2, 2007
68
GB
Hi, I'm trying to make my site more search engine friendly by correctly handling 404 error pages. I have the following in my web.config file:

<customErrors mode="RemoteOnly" defaultRedirect="~/Error/500.htm">
<error statusCode="500" redirect="~/Error/500.htm"/>
<error statusCode="404" redirect="~/Error/404.htm"/>
</customErrors>

This works most of the time but sometimes a user enters a url ie /forum/viewpost.aspx?postid=99 and although the viewpost.aspx page exists i want to throw a 404 error. I figured i could do a redirect to my 404 page (if the post could not be found) and then manually update the header status code in my 404 page, like so:

protected void Page_Load(object sender, EventArgs e)
{
// Add response header
Response.Buffer = true;
Response.StatusCode = 404;
Response.Status = "404 Not Found";
}

But this keeps returning a status code of 302. I have tried commenting out the response.buffer line but this did not work either.

Really appreciate if someone could help. Thanks
 
How about redirecting to a page that does not exist?

HTH
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top