我熟悉 C#/.net,但对 .net mvc 和 razor 东西还很陌生, 我想做的是,我想从 View 中调用 Controller 中的方法以将字符串路径呈现为二进制, 所以我在 View 中这样做:
<img src="@Url.Action("ReadFile", "FLK", new { path = Model.Pict })" />
Controller 中的这个:
public void ReadFile(string path)
{
Response.ContentType = "image";
Response.BinaryWrite(System.IO.File.ReadAllBytes(path));
}
但是当我在 Controller 中放置一个调试点时,我的调试点从未触发过,有什么线索吗?需要一些建议谢谢。
最佳答案
Controller 中的 ReadFile()
方法应该是这样的 -
public ActionResult ReadFile(string path)
{
byte[] imgBytes = System.IO.File.ReadAllBytes(path);
return File(imgBytes, "image/png");
}
在这里,您的操作方法将图像文件读入字节数组,然后使用 ActionResult 基类的 File() 方法将内容发送给调用者。
所以你可以像这样在你的 View 中使用它 -
<img src='@Url.Action("ReadFile", new { path = Model.Pict }))'/>
关于c# - 从 MVC View 调用方法不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25990517/