为什么我的图像缓存? [英] Why aren't my images caching?
问题描述
我有一个ASP.NET MVC3应用程序设置。有返回影像控制器和我已经加入以下内容:
I have an ASP.NET MVC3 application setup. There is a controller that returns back images and I have added the following:
[OutputCache(Duration = 3600, VaryByParam = "id;width", Order = 1000, Location = OutputCacheLocation.Client)]
public ActionResult Get(string id, int width)
{ ... }
但是,当我检查出这些图片它们都具有标头说,HTTP响应缓存控制:无缓存和到期:-1,这意味着浏览器缓存从不其中
But when I check out the HTTP Response on these images they all have headers that say "cache-control: no-cache" and "expires: -1" which means the browser is never caching them.
我在寻找各地,我找不到原因的响应告诉浏览器不缓存他们什么。我甚至尝试工作了我自己的属性,它所做的:
I'm looking all around and I can't find anything on why the response is telling the browser not to cache them. I even tried working up my own attribute that did:
public class ContentExpiresHeader : ActionFilterAttribute
{
public override void OnResultExecuted(ResultExecutedContext filterContext)
{
var cache = filterContext.HttpContext.Response.Cache;
cache.SetExpires(DateTime.Now.AddYears(1));
cache.SetCacheability(HttpCacheability.Private);
cache.SetLastModifiedFromFileDependencies();
base.OnResultExecuted(filterContext);
}
}
但没有得到任何地方我不是。
but that didn't get me anywhere either.
任何帮助是AP preciated。
Any help is appreciated.
更新:我开始想,这一定是一个IIS设置的地方,它是将无缓存和压倒一切的。我似乎无法找到任何东西,但。唯一奇怪的是,如果我看一看在的缓存的我叫。设置后变量...的状态()方法内部变量尚未更新。我本来期望的东西改变,但他们仍然显示无缓存。
UPDATE: I'm starting to think this has got to be an IIS setting somewhere that is adding the no-cache and overriding. I can't seem to find anything, though. The only odd thing is that if I take a look at the state of the cache variable after I've called the .Set...() methods the internal variables have not been updated. I would have expected something to change but they're still showing "no-cache".
更新2:我要补充,这种方法的返回是:
UPDATE 2: I should add that the return of this method is a:
return File(...);
更新3:我也发现这个(http://dotnetslackers.com/articles/aspnet/ASP-NET-MVC-3-Controller-for-Serving-Images.aspx),并试图没有任何运气实现它。仍然得到对图像的响应头无缓存选项。
UPDATE 3: I also found this (http://dotnetslackers.com/articles/aspnet/ASP-NET-MVC-3-Controller-for-Serving-Images.aspx) and tried implementing it without any luck. Still getting the no-cache options on the response header for the images.
更新4:只是不得不检查服务器设置...如果我绕过我的控制器,直接进入到一个图像文件的服务器上,那么它的高速缓存,并在响应头部正确的缓存设置
UPDATE 4: Just had to check server settings... if I bypass my controller and go straight to an image file on the server, then it DOES cache and has the correct caching settings in the response header.
更新5(是的,越来越疯狂):创建一个全新的项目MVC3,只是取得了一个控制器,它缓存就好了。所以,我已经得到了即时code,它是将这个编译之外的东西:no-cache的东西,对我的生活我无法弄清楚它会是什么。 = - /
UPDATE 5 (yeah, getting crazy): Created a brand new MVC3 project and just made the one controller and it cached just fine. So I've got something outside the immediate code that is adding this pragma:no-cache stuff and for the life of me I can't figure out what it'd be. =-/
推荐答案
发现问题!而且这是我在一段时间看到的最奇怪的事情。
Found the problem! And it's the weirdest thing I've seen in a while.
我使用的是安装过程中SocialAuth网和地方我增加了它的system.webServer模块并设置runAllManagedModulesForAllRequests =真。我想,呵呵,不知道这是否以某种方式导致它,因为我无法重现此特定的应用程序之外的问题。低不料,注释掉的配置那部分,我的图片开始缓存! Hoorah!
I am using SocialAuth-net and somewhere during the setup I added the system.webServer module for it and set runAllManagedModulesForAllRequests=true. I thought, "huh, wonder if that's causing it somehow", as I couldn't reproduce the problem outside this particular app. Low and behold, commenting out that section of the config and my images started caching! Hoorah!
但是,它变得怪异。我解开我的配置变化,神清气爽,现在我仍然得到缓存。我不能告诉你如何系统的许多重置我没有做改变,但不知何故临时从管线上拆下这些模块似乎已经解决了这个问题。
But, it gets weirder. I undid my config changes, refreshed and now I'm still getting caching. I can't tell you how many resets of the system I've done with no change but somehow temporarily removing these modules from the pipeline seems to have resolved this problem.
我可以跟踪下来的SocialAuthHttpModule,如果我删除它SocialAuth网似乎仍然工作,但缓存被可靠地恢复。十分怪异。
这篇关于为什么我的图像缓存?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!