【问题标题】:Correct way to show an image that comes from a controller显示来自控制器的图像的正确方法
【发布时间】:2013-11-01 13:57:00
【问题描述】:

我有一个加载图像文件的控制器:

    //
    // GET: /MyController/Image/id
    public ActionResult Image(string id)
    {
        var dir = @"C:\Temp\Images";
        var path = Path.Combine(dir, id + ".png");
        return File(path, "image/png");
    }

在与动作MyController/Page/id 不同的视图中,我匆忙整理了一些 html 来显示图像:

<img src="../image/@Model.Name" />

然后还有一个链接:

<a href="../image/@Model.Name">
  <img src="../image/@Model.Name" />
</a>

它有效,但我正在寻找使用@Html 的更好方法,因此 resharper 可以帮助导航和验证,对于上述两个。我试过这个:

@Html.RenderAction("Image", "MyController", new { id = Model.Name })

但这不能编译,它说Expression must return a value to render

【问题讨论】:

    标签: asp.net-mvc-4 razor-2


    【解决方案1】:

    我认为您只是在寻找Url.Action(),它会返回一个指向操作的 URL:

    <img src="@Url.Action("Image", "ImageController", new { id = Model.ImageID })" />
    

    【讨论】:

      【解决方案2】:

      你可以在 src 属性中使用Url.Action()

       <img src='@Url.Action("Image", "ImageController", new { imageId = Model.Id })' alt='MyImage' />
      

      【讨论】:

        猜你喜欢
        • 2017-06-07
        • 1970-01-01
        • 1970-01-01
        • 2014-10-05
        • 1970-01-01
        • 1970-01-01
        • 2015-08-18
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多