I have a Controller that pulls images from a database, re-sizes them, caches the result on disk, and spits the image out as a Content() result.
Recent I have added support for a “Scrape-buster” code on my site. That is, I take a hash of a unique code attached to each image plus some salt, and pass the first few characters of that hash to the user, for confirmation during retrieval. This allows me to prevent people from scraping every image off of the site. (Without being logged in, and scraping the HTML as well, that is.)
Anyways, If the ScrapeBuster code is incorrect, I would like to return a 404 error from my controller. Is there a built-in way to do this, or am I looking at building a custom ActionResult?
The simplest (probably not the cleanest) way is to throw an
HttpException:You should make sure no one in the call stack is catching the exception. This way, output will look like the normal ASP.NET Yellow Screen of Death 404.
The cleaner way is to return a custom result: