显示来自控制器的图像的正确方法 [英] Correct way to show an image that comes from a controller
本文介绍了显示来自控制器的图像的正确方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个加载图像文件的控制器:
I have a controller that loads an image file:
//
// 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以显示图像:
In a separate view from action MyController/Page/id
I have hastily hashed up some html to show the image:
<img src="../image/@Model.Name" />
然后还有一个链接:
<a href="../image/@Model.Name">
<img src="../image/@Model.Name" />
</a>
它可以工作,但是我正在寻找一种使用@Html
的更好的方法,因此对于以上两个方面,resharper可以帮助进行导航和验证.我已经尝试过了:
It works, but I am looking for a better way using @Html
so resharper can help with navigation and validation, for the two above. I've tried this:
@Html.RenderAction("Image", "MyController", new { id = Model.Name })
但这不会编译,它表示为Expression must return a value to render
.
But this doesn't compile, it says Expression must return a value to render
.
推荐答案
我认为您只是在寻找Url.Action()
,它将返回URL到Action:
I think you're just looking for Url.Action()
, which returns an URL to an Action:
<img src="@Url.Action("Image", "ImageController", new { id = Model.ImageID })" />
这篇关于显示来自控制器的图像的正确方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文