【发布时间】:2010-10-11 19:18:24
【问题描述】:
我有一个存储图像的数据库,可以通过主键 ID 访问(呕吐,我知道,但它不在我的控制范围内)。
我有一个非常标准的 Asp .net Mvc 控制器,它读取数据库,如果在数据库中找到一行,它会将图像字节作为 FileResult 返回。如果没有找到一行,我有一个自定义操作结果,将响应状态设置为 404。我不抛出异常,而是让 IIS 处理 404 状态并返回一个静态文件。
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;
}
}
最近,我们在 Firefox v3.6.1 上进行测试,并注意到浏览器对每个图像发出多个请求(准确地说是 3 个),因此我们的控制器工厂被命中 3 次,并从我们的数据库访问数据库 3 次动作方法。此行为仅在 Firefox 中发生(IE、chrome 正常)。
我的问题是,将 Http Cache Headers 添加到我的 ContentMediaNotFoundResult 中的 404 响应是否有意义,这样我的操作方法就不会不断地访问数据库。
【问题讨论】:
标签: asp.net-mvc http iis-7