2

我很难弄清楚如何从 blob 流中缓存图像。
这是我的第一个代码,它可以下载返回的图像文件:

HTML 部分:

<img src="@Url.Action("GetImage", "Asset")?photoId=@SessionData.ProfilePic.PhotoID&type=3"/>  

控制器:

public ActionResult GetImage(long photoId, PhotoType type)
{   
    byte[] img = <Code that fetches image byte from blob stream>;
    return File(img, "image/png");            
}

但这无法在浏览器中缓存。
我在想,如果 url 看起来像这样:

http://stackoverflow.com/Asset/GetImage/1/2  

图像现在可以缓存了吗?
所以我创建了另一条路线:

routes.MapRoute(
    "Upload",
    "Asset/GetImage/{photoId}/{type}",
    new { controller = "Asset", action = "GetImage" }
);

并像这样访问图像:

http://stackoverflow.com/Asset/GetImage/1/2
<img src="http://stackoverflow.com/Asset/GetImage/1/2" />

但我总是得到:

Failed to load resource: the server responded with a status of 404 (Not Found)   

现在我的问题是:

  1. 我是否认为当 url 像这样格式化时,http://stackoverflow.com/Asset/GetImage/1/2它现在可以被浏览器缓存?
  2. 为什么我会收到此错误?Failed to load resource: the server responded with a status of 404 (Not Found)我的路线有什么问题?

希望有人能帮忙。

编辑

这是我的完整路线:

routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

routes.MapRoute(
    "Upload",
    "Asset/GetImage/{photoId}/{type}",
    new { controller = "Asset", action = "GetImage" }
);

var route = routes.MapRoute(
        "Gallery",
        "",
        new { controller = "Auth", action = "Login" },
        new[] { "Project.Areas.Gallery.Controllers" }
    );

route.DataTokens["area"] = "Gallery";  

仍然出现错误Failed to load resource: the server responded with a status of 404 (Not Found),可能是什么问题?

编辑2

我使用的元标记列表:

<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<meta name="description" content="">
<meta name="author" content="">
<meta name="viewport" content="width=device-width,initial-scale=1">
<meta http-equiv="imagetoolbar" content="false">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
4

1 回答 1

2

以下对我有用

动作方法

[OutputCache(Duration = 20, VaryByParam = "photoId")]
public ActionResult GetImage(long photoId, long type)
{
    byte[] img = System.IO.File.ReadAllBytes(Server.MapPath("~/Images/aspNetHome.png"));
    return File(img, "image/png");
}

HTML

<img src="~/Home/GetImage/1/2" alt="" />

路由添加到 Global asax

routes.MapRoute(
    "Upload",
    "Home/GetImage/{photoId}/{type}",
    new { controller = "Home", action = "GetImage" }
);

来自 Fiddler 的响应标头

引用:http://localhost/MvcApplicationImageTest/Home/GetImage/1/2*

HTTP/1.1 200 OK
Cache-Control: private, max-age=20
Content-Type: image/png
Expires: Wed, 01 Aug 2012 05:08:25 GMT
Last-Modified: Wed, 01 Aug 2012 05:08:05 GMT
Server: Microsoft-IIS/7.5
X-AspNetMvc-Version: 4.0
X-AspNet-Version: 4.0.30319
X-Powered-By: ASP.NET
Date: Wed, 01 Aug 2012 05:08:10 GMT
Content-Length: 3736

编辑以下评论

移除了 OutputCacheAttribute

HTTP/1.1 200 OK
Cache-Control: private
Content-Type: image/png
Server: Microsoft-IIS/7.5
X-AspNetMvc-Version: 4.0
X-AspNet-Version: 4.0.30319
X-Powered-By: ASP.NET
Date: Thu, 02 Aug 2012 04:52:02 GMT
Content-Length: 3736

正如您在标题中看到的那样,Cache-Control只显示private而不是Cache-Control: no-cache, Expires: -1, Pragma: no-cache

我现在相信您必须以某种方式明确禁用缓存?您的视图或布局页面中是否有任何META标签?例如

<meta http-equiv="cache-control" content="no-cache" />

<meta http-equiv="pragma" content="no-cache" />

<meta http-equiv="expires" content="-1" />
于 2012-08-01T05:27:04.570 回答