问题:即使图像路径正确,也无法使用Razor View在浏览器上渲染图像。此外,图像没有损坏,因为HTML文件可以正确地引用图像。
代码:
索引.cshtml
@{
ViewData["Title"] = "Home Page";
var workingDirectory = System.Environment.CurrentDirectory;
var projectDirectory = System.IO.Directory.GetParent(workingDirectory).Parent.Parent.FullName;
var path = System.IO.Path.Combine(workingDirectory, "wwwroot", "images", "sampleImage.png");
}
<div class="text-center">
<p>@System.IO.File.Exists(path)</p> // returns True
<img src="@path" alt="error" />
</div>
如果有人能帮助解决这个问题,那就太好了!!
File.Exists在服务器上运行。src是从客户端请求的。尝试
src = "@Url.Content("~/images/sampleImage.png")"