[英]How do I add caching for images used in my ASP.NET MVC web app?
I have read this http://madskristensen.net/post/add-expires-header-for-images and https://technet.microsoft.com/en-us/library/cc770661.aspx and other similar articles, who suggest to put this我已阅读此http://madskristensen.net/post/add-expires-header-for-images和https://technet.microsoft.com/en-us/library/cc770661.aspx和其他类似文章,他们建议把这个
<staticContent>
<clientCache httpExpires="Sun, 29 Mar 2020 00:00:00 GMT" cacheControlMode="UseExpires" />
</staticContent>
But even after that the images are not fetched from cache and 200 ok response is sent, that is a request to the server is made.但即使在这之后,图像也没有从缓存中获取,并且发送了 200 ok 响应,这就是向服务器发出的请求。 I want no request to be made for x hours/days coz these image wont change for a long time.
我不希望在 x 小时/天内发出任何请求,因为这些图像在很长一段时间内都不会改变。
How do I do this ?我该怎么做呢 ?
The following configuration should cause browsers to cache your images for an entire year:以下配置应该会导致浏览器将您的图像缓存一整年:
<staticContent>
<clientCache cacheControlMode="UseMaxAge" cacheControlMaxAge="365.00:00:00" />
</staticContent>
<httpProtocol>
<customHeaders>
<add name="Cache-Control" value="public" />
</customHeaders>
</httpProtocol>
You just need to make sure your images are being served as static file types.您只需要确保您的图像作为静态文件类型提供。 There is no way of forcing a browser to not send a request to the server ie;
没有办法强制浏览器不向服务器发送请求,即; the user performs a hard fresh.
用户执行硬新鲜。
You can wrap the above configuration in a location node so as to only affect images that site in a certain path:您可以将上述配置包装在位置节点中,以便仅影响位于特定路径中的图像:
<location path="Content">
<system.webServer>
<staticContent>
<clientCache cacheControlMode="UseMaxAge" cacheControlMaxAge="365.00:00:00" />
</staticContent>
<httpProtocol>
<customHeaders>
<add name="Cache-Control" value="public" />
</customHeaders>
</httpProtocol>
</system.webServer>
</location>
The above configuration would add an HTTP cache header directive for all images hosted at http://www.example.com/Content/Images/ *上述配置将为托管在http://www.example.com/Content/Images/的所有图像添加 HTTP 缓存标头指令 *
You should create a configurable appsetting that is passed in to such images URI's as a query string parameter.您应该创建一个可配置的应用设置,将其作为查询字符串参数传递给此类图像 URI。 This will allow you to clear all the clients to send a request the images from your server: (We want control over this as cached images can be problematic)
这将允许您清除所有客户端以从您的服务器发送图像请求:(我们希望对此进行控制,因为缓存的图像可能会出现问题)
<img src="/Content/Images/MyImage.jpg?version=<%# Global.ImageVersion %>" />
More on caching headers (Cache-Control) here http://www.mobify.com/blog/beginners-guide-to-http-cache-headers/更多关于缓存头(Cache-Control)的信息在这里http://www.mobify.com/blog/beginners-guide-to-http-cache-headers/
I hope that helps!我希望这会有所帮助!
Asp.net Mvc page Index.cshtml: Asp.net Mvc 页面 Index.cshtml:
<div class="title">
<img src="../../Content/title.png?ver=@DateTime.Now.ToString("ssfff")">
</div>
Output Home/Index:输出主页/索引:
<div class="title">
<img src="../../Content/title.png?ver=13388">
</div>
this page will refresh img src every time.此页面每次都会刷新 img src。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.