我试图在我的回复中添加一个“max-age”标题。它在我的 Visual Studio 开发服务器上运行良好,但是一旦我将应用程序移动到 IIS(尝试了本地 IIS Express 和服务器上的 IIS)- 标题就消失了。
我的代码:
Response.Cache.SetCacheability(HttpCacheability.Public);
Response.Cache.SetMaxAge(new TimeSpan(1, 0, 0, 0));
VS 开发服务器响应(一切正常):
HTTP/1.1 200 OK
Server: ASP.NET Development Server/10.0.0.0
Date: Fri, 07 Jan 2011 14:55:04 GMT
X-AspNet-Version: 2.0.50727
Cache-Control: public, max-age=86400
IIS7 响应
HTTP/1.1 200 OK
Server: Microsoft-IIS/7.5
Date: Fri, 07 Jan 2011 15:00:54 GMT
X-AspNet-Version: 2.0.50727
Cache-Control: public
附言。它是一个 ASHX 处理程序,如果重要的话......
最佳答案
更新: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”,如果 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");
}
}
关于c# - Cache.SetMaxAge 在 IIS 下不工作,在 VS Dev Srv 下工作正常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4626780/