【发布时间】:2017-12-06 14:28:31
【问题描述】:
我正在尝试使用此视图在页面上显示一些图像
Details.cshtml
@foreach (var imagePath in Model.Images)
{
<img src="@Url.Action("GetImage", "Receiving", new { @imagePath = imagePath })" />
}
ReceivingController.cs
public ActionResult GetImage(string imagePath)
{
var folder = ConfigurationManager.AppSettings["ImagesPath"];
var path = Path.Combine(folder, imagePath);
byte[] image = System.IO.File.ReadAllBytes(path);
return File(image, "image/jpg");
}
但是当我在控制器中设置断点时,它永远不会被击中,当然图像也无法加载。
我可以看到从视图传递的值是路径,例如"C:\\inetpub\\wwwroot\\SiteName\\Images\\3000\\1\\1\\2.jpg"
我尝试更改 @Url.Action' to@Html.Action` 只是因为我不知道还能尝试什么,但出现错误
使用自定义 TextWriter 时,OutputStream 不可用
我不明白为什么没有调用控制器。
我注意到,如果我只是导航到 url Receiving\GetImage\whyareyounotworking,就会触发控制器操作。
我尝试添加如下路线,但没有任何变化
routes.MapRoute(
name: "GetImage",
url: "receiving/GetImage/{imagePath}",
defaults: new { controller = "Receiving", action = "GetImage" }
);
【问题讨论】:
-
为什么不将
imagePath的值设置为正确的值 - 例如"/Images/YourFileName.jpg"?问题是路由包含.(参考this answer) -
@StephenMuecke 我已经为此苦苦挣扎多年了。该图像位于仅内部站点的单独网站/应用程序池中。但是,这个独立的、面向外部的站点需要提供来自内部站点的图像。我不确定如何构建相对 url 来做到这一点,或者它是否可能。似乎只是显示一些图像应该是一件容易的事,但我似乎做不到..
标签: c# asp.net-mvc