简体   繁体   English

如何为 ASP.NET MVC Web 应用程序中使用的图像添加缓存?

[英]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-imageshttps://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.

相关问题 如何在MVC 3 Web应用程序中使用ASP.NET Session对象? - How do I use ASP.NET Session object in my MVC 3 web app? 如何向我的 MVC asp.net Core 应用程序添加排序功能? - How do I add a sorting feature to my MVC asp.net Core app? 如何将材料 styles 添加到 ASP.NET MVC web 应用程序? - How to add material styles to ASP.NET MVC web app? 如何将ASP.NET MVC Web应用程序的标识/身份验证部分移到类库中? - How can I move the identity/authentication part of my ASP.NET MVC web app into a class library? 如何在我的asp.net网络应用程序中添加httpmodule代码块? - how to add a httpmodule codeblock to my asp.net web app? 如何在MVC中使用ASP.NET Web窗体? - How do I use ASP.NET Web Forms in MVC? 如何在 ASP.NET MVC 3.0 项目中以正确的方式缓存? - How do i do caching the right way in a ASP.NET MVC 3.0 project? 如何将jQuery UI添加到ASP.NET MVC 4中的捆绑软件中? - How do I add jQuery UI to my bundle in ASP.NET MVC 4? 如何解决在我的ASP.Net MVC应用程序中的iisreset之后发生的AntiForgeryToken异常? - How do I solve an AntiForgeryToken exception that occurs after an iisreset in my ASP.Net MVC app? 如何在Web服务器的内存中为ASP.NET MVC Web应用程序缓存图像? - How to cache images in memory on the web server for an ASP.NET MVC web app?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM