Url.Action无法调用控制器方法

时间:2017-12-06 06:23:24

标签: c# asp.net-mvc

我正在尝试使用此视图在页面上显示一些图像

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不可用

我不明白为什么没有调用控制器。

我注意到如果我只是导航到网址Receiving\GetImage\whyareyounotworking,则会触发控制器操作。

我尝试添加如下路线,但没有变化

routes.MapRoute(
    name: "GetImage",
    url: "receiving/GetImage/{imagePath}",
    defaults: new { controller = "Receiving", action = "GetImage" }
);

2 个答案:

答案 0 :(得分:0)

删除它:

@imagePath

所以你会有这样的url.action:

<img src="@Url.Action("GetImage", "Receiving", new { imagePath = imagePath })" />

答案 1 :(得分:0)

这似乎与传入的字符串有关。

从文件路径的末尾删除func writeToFile(urlString: String) { let file = "/saveURL.txt" //this is the file. we will write to and read from it if let dir = NSSearchPathForDirectoriesInDomains(FileManager.SearchPathDirectory.documentDirectory, FileManager.SearchPathDomainMask.allDomainsMask, true).first { let path = dir + file print(path) do { try urlString.write(toFile: path, atomically: false, encoding: String.Encoding.utf8) } catch {/* error handling here */} } } 之后,在传回控制器之前,我的断点现在命中了。