How to programmatically serve HTTP 404 from ASP.NET code-behind and redirect to 404 page?
===

I've got an ASP.NET app that sometimes redirects to a 404 page if requested content isn't found.  This is done something like this:

    var data = MyDatabase.RetrieveData(dataID);
    if (data == null)
       Response.Redirect("~/My404Page.aspx");

This works great for users.  But it's resulting in `302 Found` (a redirect), not `404 Not Found`, which is not ideal for search crawlers.  I'd like to return a 404 status code and still display helpful content to the user.

The famous [internet's most awkard 404 page](http://visitsteve.com/404.html) does this; if you enter a bad URL in that domain and watch the result with something like Fiddler, you actually get a 404 code back.

I've tried this:

    Response.StatusCode = 404;

This sets the 404 code properly, but continues rendering the page.  I've also tried this:

    throw new HttpException(404, "Not Found");

But this does the same thing it would with any other uncaught exception (redirects via `302` to a server error page).

I've figured out a way to solve this in my app via some custom logic that involves setting the status code myself and writing a javascript redirect to the response.  But what I'd like to have is a way to express in code-behind that this request should stop processing and redirect to the 404 error page (which I believe can be specified either in IIS or web.config).  Is this possible?