I have a database that stores images which can be access via a primary key ID (vomit, I know, but it's out of my control).
I have a pretty standard Asp .net Mvc Controller that reads the database and if a row is found in the database it returns the image bytes as a FileResult. If a row is not found, I have a custom action result that sets the response status to 404. I do not throw an exception, but instead let IIS handle the 404 status and return a static file.
public class ContentMediaNotFoundResult : ActionResult
{
public ContentMediaNotFoundResult() { }
public override void ExecuteResult(ControllerContext context)
{
//-- Here the only thing we are doing is setting the Response code to 404 and then we are going to let IIS service the request
//-- based on the settings in system.webServer/httpErrors (which may come from the root .config, so be careful)
context.HttpContext.Response.StatusCode = 404;
}
}
Recently, we were testing on Firefox v3.6.1 and were noticing the browser was making multiple requests (3 to be precise) for each image, hence our controller factory was being hit 3 times and going to the database 3 times from our action method. This behavior was only happneing from Firefox (IE, chrome were normal).
My question is, does it make sense to add Http Cache Headers to 404 responses in my ContentMediaNotFoundResult so that my action method doesn't hit the database continually.