如何为ASP中使用的图像添加缓存。净MVC web应用程序?

时间:2022-05-07 06:14:19

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-head -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. I want no request to be made for x hours/days coz these image wont change for a long time.

但是即使在没有从缓存中获取图像并发送200个ok响应之后,这也是对服务器的请求。我不希望在x小时/天内发出任何请求,因为这些图像在很长一段时间内不会改变。

How do I do this ?

我该怎么做呢?

1 个解决方案

#1


3  

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.

您只需要确保您的图像是静态文件类型。没有办法强迫浏览器不向服务器ie发送请求;用户执行硬刷新。

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

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

<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-guidetohttp -cache-headers/

I hope that helps!

我希望会有帮助!

#1


3  

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.

您只需要确保您的图像是静态文件类型。没有办法强迫浏览器不向服务器ie发送请求;用户执行硬刷新。

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

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

<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-guidetohttp -cache-headers/

I hope that helps!

我希望会有帮助!