C# 如何缓存HttpResponseMessage返回的映像
我在缓存HttpResponseMessage返回的图像时遇到问题 通过URL访问文件:C# 如何缓存HttpResponseMessage返回的映像,c#,.net,http,caching,httpresponse,C#,.net,Http,Caching,Httpresponse,我在缓存HttpResponseMessage返回的图像时遇到问题 通过URL访问文件: http://localhost:[service port]/[file GUID]?Adapter=[adapter type] 例如: 我将CacheControl头添加到HttpResponseMessage: new CacheControlHeaderValue() { Public = true,
http://localhost:[service port]/[file GUID]?Adapter=[adapter type]
例如:
我将CacheControl头添加到HttpResponseMessage:
new CacheControlHeaderValue()
{
Public = true,
MaxAge = TimeSpan.FromSeconds(60)
};
响应的两个缓存控制头都在浏览器(Chrome)中正确显示,但该url的每次刷新都会执行在服务器上检索图像的方法,而不是从缓存中提供此图片
我是否缺少某些内容(IIS配置、url表单) 您想将映像添加到IIS缓存,并想知道为什么要多次下载映像?如果您有多个需要相同资源的客户端,则IIS缓存非常有用 您可以使用(命令行)检查以查看当前的缓存: 您很可能希望浏览器缓存-相同的客户端在短时间内反复请求相同的文件。 以下情况会导致浏览器缓存您的图像:
<staticContent>
<clientCache cacheControlMode="UseMaxAge" cacheControlMaxAge="1.00:00:00" />
</staticContent>
<httpProtocol>
<customHeaders>
<add name="Cache-Control" value="public" />
</customHeaders>
</httpProtocol>
我使用以下代码:
var age = new TimeSpan(cacheTime, 0, 0);
response.Headers.CacheControl = new CacheControlHeaderValue()
{
MaxAge = age,
Public = false,
NoCache = false,
Private = true,
};
response.Content.Headers.Expires = DateTime.UtcNow.Add(age);
我不确定是因为过期,还是因为我使用了24小时作为缓存时间。感谢您的回复,它帮助我找到了问题所在。当url位于html中img元素的src属性中时,设置缓存头是有效的。但执行url直接绕过缓存。
<staticContent>
<clientCache cacheControlMode="UseMaxAge" cacheControlMaxAge="1.00:00:00" />
</staticContent>
<httpProtocol>
<customHeaders>
<add name="Cache-Control" value="public" />
</customHeaders>
</httpProtocol>
var age = new TimeSpan(cacheTime, 0, 0);
response.Headers.CacheControl = new CacheControlHeaderValue()
{
MaxAge = age,
Public = false,
NoCache = false,
Private = true,
};
response.Content.Headers.Expires = DateTime.UtcNow.Add(age);