加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > asp.Net > 正文

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

发布时间:2020-12-16 07:02:07 所属栏目:asp.Net 来源:网络整理
导读:我已经阅读了这篇 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"
我已经阅读了这篇 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>

但即使在那之后,图像也没有从缓存中获取并且发送了200 ok响应,这就是对服务器的请求.我想要x小时/天没有要求,因为这些图像很长时间都不会改变.

我该怎么做呢 ?

解决方法

以下配置应使浏览器将图像缓存整整一年:

<staticContent>
    <clientCache cacheControlMode="UseMaxAge" cacheControlMaxAge="365.00:00:00" />
</staticContent>
<httpProtocol>
    <customHeaders>
        <add name="Cache-Control" value="public" />
    </customHeaders>
</httpProtocol>

您只需确保将图像作为静态文件类型提供.没有办法强迫浏览器不向服务器发送请求,即;用户表现得很辛苦.

您可以将上述配置包装在位置节点中,以便仅影响特定路径中站点的图像:

<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>

上述配置将为http://www.example.com/Content/Images/ *上托管的所有图像添加HTTP缓存头指令

您应该创建一个可配置的appsetting,它作为查询字符串参数传递给这样的图像URI.这将允许您清除所有客户端以从服务器发送请求图像:(我们希望控制这个,因为缓存的图像可能有问题)

<img src="/Content/Images/MyImage.jpg?version=<%# Global.ImageVersion %>" />

有关缓存标头(Cache-Control)的更多信息,请参见http://www.mobify.com/blog/beginners-guide-to-http-cache-headers/

我希望有所帮助!

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读