C# 缓存MVC操作返回的映像

C# 缓存MVC操作返回的映像,c#,caching,model-view-controller,C#,Caching,Model View Controller,我创建了一个从数据库检索图像的图像控制器。它们使用的是非参数化URL(/Images/showcontegory/1)。我的问题来自于它们没有被缓存。我已经设置了我的方法来设置可缓存性、过期时间和最大期限。但它仍然不会缓存图像。我还在这个方法上使用OutputCaching属性[OutputCache(Duration=int.MaxValue,VaryByParam=“id”)]。我从未从服务器获得未修改的响应。这里有我遗漏的东西吗 Request.RequestContext.HttpCon

我创建了一个从数据库检索图像的图像控制器。它们使用的是非参数化URL(/Images/showcontegory/1)。我的问题来自于它们没有被缓存。我已经设置了我的方法来设置可缓存性、过期时间和最大期限。但它仍然不会缓存图像。我还在这个方法上使用OutputCaching属性
[OutputCache(Duration=int.MaxValue,VaryByParam=“id”)]
。我从未从服务器获得未修改的响应。这里有我遗漏的东西吗

Request.RequestContext.HttpContext.Response.Cache.SetCacheability(HttpCacheability.Public);
Request.RequestContext.HttpContext.Response.Cache.SetExpires(DateTime.Now.AddDays(7));
Request.RequestContext.HttpContext.Response.Cache.SetMaxAge(new TimeSpan(7, 0, 0, 0));

byte[] image = (from c in db.Categories
                where c.ID == id
                select c.Image).FirstOrDefault();

Image img;
Size size = new Size(225, 175);

using (MemoryStream ms = new MemoryStream())
{
    if (image == null)
    {
        string path = Server.MapPath(@"~\Images\NoImage.png");
        img = Image.FromFile(path);
    }
    else
        img = Image.FromStream(new MemoryStream(image, 0, image.Length));

    img = (Image)new Bitmap(img, size);
    img.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
    image = ms.ToArray();
}

return File(image, "image/jpeg");
答复:

HTTP/1.1 200 OK
Cache-Control: private, max-age=604800
Content-Type: image/jpeg
Expires: Thu, 16 Oct 2014 15:09:21 GMT
Server: Microsoft-IIS/8.0
X-AspNetMvc-Version: 4.0
X-AspNet-Version: 4.0.30319
Persistent-Auth: true
X-Powered-By: ASP.NET
Date: Thu, 09 Oct 2014 15:09:21 GMT
Content-Length: 40317

你试过用这个吗?@DavidG是的,我忘了提那个。谢谢你的提醒。这有用吗@戴维德:是的,这真的很管用。谢谢你,我不知道为什么我的搜索没有早点出现。