Throwing a 404 when rewriting urls

  • Thread starter Thread starter IanW
  • Start date Start date
I

IanW

Hi,

I'd just like some advice on the best way to throw a 404 when
rewriting urls. The scenario is as follows:

My url is /products/redwidgets

This redirects fine to ~products.aspx?id=redwidgets

I then do a db lookup to see if redwidgets exists, but it doesn't. At
this point I want to throw a 404 error.

Should I create a 404.aspx page and server.transfer to that, or is
there some other way?

Thanks.
 
So Do I put this code in the products.aspx page?

HttpContext.Current.Response.StatusCode = 404;
HttpContext.Current.Response.SuppressContent = true;
HttpContext.Current.ApplicationInstance.CompleteRequest();
return;

I have the following in web.config, but the current solution does not
redirect to the actual error page. How would I accomplish that?

<customErrors mode="Off">
<error statusCode="404" redirect="~/404.aspx"/>
</customErrors>
 
Back
Top