c# - 如何使用自定义路由缓存图像文件返回图像文件?

标签 c# caching routes asp.net-mvc

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

HTML 部分:

<img src="@Url.Action("GetImage", "Asset")<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="cbf4bba3a4bfa482aff68b98aeb8b8a2a4a58faabfaae59bb9a4ada2a7ae9ba2a8e59ba3a4bfa4828f" rel="noreferrer noopener nofollow">[email protected]</a>&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)   

现在我的问题是:

  1. 我的想法是否正确,当网址的格式如下 http://stackoverflow.com/Asset/GetImage/1/2 时,它现在可以被浏览器缓存?
  2. 为什么我会收到此错误? 无法加载资源:服务器响应状态为 404(未找到) 我的路线出了什么问题?

希望有人能帮忙。

编辑

这是我的完整路线:

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";  

仍然收到错误无法加载资源:服务器响应状态为 404(未找到),可能出了什么问题?

编辑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" />

最佳答案

以下内容适合我

操作方法

[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 的响应 header

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 : 无缓存

我现在相信您必须以某种方式显式禁用缓存?您的 View 或布局页面中是否有 META 标记?例如

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

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

<meta http-equiv="expires" content="-1" />

关于c# - 如何使用自定义路由缓存图像文件返回图像文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11752599/

相关文章:

PHP 正则表达式匹配字符串中的组

c# - WPF DataTrigger 绑定(bind)不起作用

c# - 需要使用 jQuery getJSON 的工作示例

laravel - 在 Redis 中使用标签缓存键会降低性能

session - 清除所有网站缓存?

php - Silex,您可以检查路由是否与回调匹配吗?

c# - 处理 fanart.tv Web 服务响应 JSON 和 C#

c# - 在 DataGridView 中选择/取消选择行

android - 在 Android 上 Context.getCacheDir() 似乎返回了一个我无法在其中创建子目录的目录

ruby-on-rails - rails 半复杂 STI 与祖先数据模型规划路线和 Controller