我很难搞清楚如何从blob流中缓存图像 这是我的第一个代码,它可以下载返回图像文件:
HTML PART:
<img src="@Url.Action("GetImage", "Asset")?photoId=@SessionData.ProfilePic.PhotoID&type=3"/>
CONTROLLER:
public ActionResult GetImage(long photoId, PhotoType type)
{
byte[] img = <Code that fetches image byte from blob stream>;
return File(img, "image/png");
}
但是这无法在浏览器中缓存 我想如果网址看起来像这样:
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)
现在我的问题是:
http://stackoverflow.com/Asset/GetImage/1/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)
,可能出现什么问题?
EDIT2
我使用的元标记列表:
<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" />
答案 0 :(得分: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="" />
路线已添加至全球税务
routes.MapRoute(
"Upload",
"Home/GetImage/{photoId}/{type}",
new { controller = "Home", action = "GetImage" }
);
来自Fiddler的响应标头
Referer: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" />