WebApi:禁用png文件的浏览器缓存

时间:2016-04-15 15:32:14

标签: caching asp.net-web-api browser-cache

我有以下WebApi操作

public class MyController : ApiController
{
    public HttpResponseMessage Get(int id)
    {
        var path = $"C:\\temp\\images\\{id}.png";
        HttpResponseMessage result = new HttpResponseMessage(HttpStatusCode.OK);
        var stream = new FileStream(path, FileMode.Open);
        result.Content = new StreamContent(stream);
        result.Content.Headers.ContentType = new MediaTypeHeaderValue("image/png");
        result.Content.Headers.Expires = DateTimeOffset.Now.AddDays(-1);
        result.Headers.CacheControl = new CacheControlHeaderValue
        {
            NoCache = true,
            Public = false,
            Private = true
        };
        return result;
    }

我通过动态地将这个img html元素附加到我的html文档来调用webapi:

<img src="http://localhost/api/mycontroller/5" />

但是,浏览器显示缓存的图像。

0 个答案:

没有答案