我试图从本地存储(而不是内容文件夹)中显示图像。我使用的图像标签看起来像这样。无法在剃刀视图中使用扩展方法
@if (Model.PreviousCoverPic != null)
{
<img src="@Url.Action("ServeImage", "Profile",new {path = Model.PreviousCoverPic})" alt="Previous Profile Pic" />
}
我提出的ServerImage方法的扩展方法,因为它会由一个以上的控制器一起使用。这里是该方法的代码:
public static ActionResult ServeImage(this System.Web.Mvc.Controller controller, string path)
{
Stream stream = new FileStream(path, FileMode.Open);
FileResult fileResult = new FileStreamResult(stream, "image/jpg");
return fileResult;
}
它返回图像的FileResult,以便它可以显示。但是,渲染视图时不显示图像。我检查了Model.PreviousCoverPic值,它不为空。我在这里错过了什么?我怎样才能实现从本地文件夹显示方法?另外,我在this问题后面跟着回答,并添加了包含扩展方法的类的名称空间,但该图像仍未在视图中呈现。
您是否在cshtml中包含了定义扩展方法的命名空间? –
我在Web.config文件中包含名称空间,以便它包含在所有cshtml文件中。 – avidProgrammer
我不认为它会这样工作。将它包含在你的cshtml中,让我知道它是如何工作的 –