我可以创建一个简单地返回图像资产的控制器吗?

我想通过控制器路由这个逻辑,每当请求如下URL时:

www.mywebsite.com/resource/image/topbanner

控制器将查找topbanner.png并将图像直接发送回客户端。

我见过这样的例子,你必须创建一个视图-我不想使用视图。我想只用控制器来做。

这可能吗?


当前回答

你可以使用HttpContext。响应并直接将内容写入它(WriteFile()可能为您工作),然后从您的动作返回ContentResult而不是ActionResult。

免责声明:我没有尝试过这个,这是基于查看可用的api。: -)

其他回答

使用MVC的发布版本,下面是我所做的:

[AcceptVerbs(HttpVerbs.Get)]
[OutputCache(CacheProfile = "CustomerImages")]
public FileResult Show(int customerId, string imageName)
{
    var path = string.Concat(ConfigData.ImagesDirectory, customerId, "\\", imageName);
    return new FileStreamResult(new FileStream(path, FileMode.Open), "image/jpeg");
}

显然,我在这里有一些关于路径构造的应用程序特定的东西,但返回FileStreamResult很好,很简单。

我做了一些性能测试,针对您每天对图像的调用(绕过控制器),平均值之间的差异仅为3毫秒(控制器的平均值为68ms,非控制器的平均值为65ms)。

我尝试了答案中提到的其他一些方法,性能的影响要大得多……一些解决方案的响应高达非控制器的6倍(其他控制器平均340ms,非控制器65ms)。

查看ContentResult。这将返回一个字符串,但可用于创建您自己的类binaryresult。

解决方案1:从图像URL在视图中呈现图像

你可以创建自己的扩展方法:

public static MvcHtmlString Image(this HtmlHelper helper,string imageUrl)
{
   string tag = "<img src='{0}'/>";
   tag = string.Format(tag,imageUrl);
   return MvcHtmlString.Create(tag);
}

然后这样使用它:

@Html.Image(@Model.ImagePath);

解决方案2:从数据库中渲染图像

创建一个返回图像数据的控制器方法,如下所示

public sealed class ImageController : Controller
{
  public ActionResult View(string id)
  {
    var image = _images.LoadImage(id); //Pull image from the database.
    if (image == null) 
      return HttpNotFound();
    return File(image.Data, image.Mime);
  }
}

并在视图中使用它:

@ { Html.RenderAction("View","Image",new {id=@Model.ImageId})}

要在任何HTML中使用此actionresult渲染的图像,请使用

<img src="http://something.com/image/view?id={imageid}>

是的,你可以返回图像

public ActionResult GetImage(string imageFileName)
{
    var path = Path.Combine(Server.MapPath("/Images"), imageFileName + ".jpg"); 
    return base.File(path, "image/jpeg");
}

(请不要忘记将此标记为答案)

使用基本控制器文件方法。

public ActionResult Image(string id)
{
    var dir = Server.MapPath("/Images");
    var path = Path.Combine(dir, id + ".jpg"); //validate the path for security or use other means to generate the path.
    return base.File(path, "image/jpeg");
}

值得一提的是,这似乎相当有效。我做了一个测试,我通过控制器(http://localhost/MyController/Image/MyImage)和直接URL (http://localhost/Images/MyImage.jpg)请求图像,结果是:

MVC:每张照片7.6毫秒 直接:每张照片6.7毫秒

注意:这是一个请求的平均时间。平均值是通过在本地机器上发出数千个请求来计算的,因此总数不应该包括网络延迟或带宽问题。