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

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

www.mywebsite.com/resource/image/topbanner

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

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

这可能吗?


当前回答

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

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毫秒

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

其他回答

更新:有比我原来的答案更好的选择。这在MVC之外工作得很好,但最好坚持使用返回图像内容的内置方法。见赞成投票的答案。

你当然可以。试试下面这些步骤:

将映像从磁盘加载到字节数组中 缓存图像,如果您希望对图像有更多的请求,并且不需要磁盘I/O(我的示例在下面没有缓存它) 通过响应改变mime类型。ContentType 响应。写出图像字节数组

下面是一些示例代码:

string pathToFile = @"C:\Documents and Settings\some_path.jpg";
byte[] imageData = File.ReadAllBytes(pathToFile);
Response.ContentType = "image/jpg";
Response.BinaryWrite(imageData);

希望有帮助!

使用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)。

是的,你可以返回图像

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

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

if (!System.IO.File.Exists(filePath))
    return SomeHelper.EmptyImageResult(); // preventing JSON GET/POST exception
else
    return new FilePathResult(filePath, contentType);

SomeHelper.EmptyImageResult()应该返回具有现有图像的FileResult(例如1x1透明)。

这是最简单的方法,如果你有文件存储在本地驱动器。 如果文件是字节[]或流-然后使用FileContentResult或FileStreamResult Dylan建议。

这对我很管用。 因为我将图像存储在SQL Server数据库上。

    [HttpGet("/image/{uuid}")]
    public IActionResult GetImageFile(string uuid) {
        ActionResult actionResult = new NotFoundResult();
        var fileImage = _db.ImageFiles.Find(uuid);
        if (fileImage != null) {
            actionResult = new FileContentResult(fileImage.Data,
                fileImage.ContentType);
        }
        return actionResult;
    }

在上面的代码片段中,_db.ImageFiles.Find(uuid)正在db (EF上下文)中搜索图像文件记录。它返回一个FileImage对象,它只是一个我为模型制作的自定义类,然后将其用作FileContentResult。

public class FileImage {
   public string Uuid { get; set; }
   public byte[] Data { get; set; }
   public string ContentType { get; set; }
}