更新:2011-03-14修复是确保您调用 SetSlidingExpiration(true)
context.Response.Cache.SetCacheability(HttpCacheability.Public);
context.Response.Cache.SetMaxAge(TimeSpan.FromMinutes(5));
context.Response.ContentType = "image/jpeg";
context.Response.Cache.SetSlidingExpiration(true);
如果您删除 OutputCache 模块,您将获得所需的结果。我认为这是一个错误。
因此,在您的 web.config 中,您将执行以下操作:
<system.webServer>
<modules runAllManagedModulesForAllRequests="true">
<remove name="OutputCache"/>
</modules>
</system.webServer>
补充:所以,还有其他信息。
- 使用 MVC 的 OutputCacheAttribute 显然没有这个问题
- 在同一个 MVC 应用程序下,不从模块中删除“OutputCache”,直接实现 if IHttpHandler 或 ActionResult 导致 s-maxage 被剥离
以下剥离了 s-maxage
public void ProcessRequest(HttpContext context)
{
using (var image = ImageUtil.RenderImage("called from IHttpHandler direct", 5, DateTime.Now))
{
context.Response.Cache.SetCacheability(HttpCacheability.Public);
context.Response.Cache.SetMaxAge(TimeSpan.FromMinutes(5));
context.Response.ContentType = "image/jpeg";
image.Save(context.Response.OutputStream, ImageFormat.Jpeg);
}
}
以下剥离了 s-maxage
public ActionResult Image2()
{
MemoryStream oStream = new MemoryStream();
using (Bitmap obmp = ImageUtil.RenderImage("Respone.Cache.Setxx calls", 5, DateTime.Now))
{
obmp.Save(oStream, ImageFormat.Jpeg);
oStream.Position = 0;
Response.Cache.SetCacheability(HttpCacheability.Public);
Response.Cache.SetMaxAge(TimeSpan.FromMinutes(5));
return new FileStreamResult(oStream, "image/jpeg");
}
}
这不是 - 去想...
[OutputCache(Location = OutputCacheLocation.Any, Duration = 300)]
public ActionResult Image1()
{
MemoryStream oStream = new MemoryStream();
using (Bitmap obmp = ImageUtil.RenderImage("called with OutputCacheAttribute", 5, DateTime.Now))
{
obmp.Save(oStream, ImageFormat.Jpeg);
oStream.Position = 0;
return new FileStreamResult(oStream, "image/jpeg");
}
}