Cache.SetMaxAge不能在IIS下工作,在VS Dev Srv下工作正常
我正在尝试为我的回复添加“max-age”标题。 它在我的Visual Studio开发服务器上工作正常,但只要我将应用程序移动到IIS(尝试本地IIS表达和服务器上的IIS) – 标题就会消失。
我的代码:
Response.Cache.SetCacheability(HttpCacheability.Public); Response.Cache.SetMaxAge(new TimeSpan(1, 0, 0, 0));
VS Dev服务器响应(一切正常):
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
PS。 它是一个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中,您将执行以下操作:
增加:所以,还有其他信息。
- 使用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"); } }
解:
在web.config
:
和IIS PC:
使用cmd转到c:\windows\system32\inetsrv
。
然后执行:
appcmd unlock config /section:staticContent
迟来的回答,但这可以帮助某人: –
Response.Cache.SetProxyMaxAge(TimeSpan.Zero);