使用图像 URL 遍历数据库,服务器错误地解释了文件路径

Looping through database with an image URL, server interprets the filepath incorrectly

我有一个 Razor 视图,我正在遍历一个包含图像 URL 等的对象。找不到图像,因为服务器似乎将本地主机地址添加到每个图像 URL。

像这样:数据库入口ImageUrl = img/shirts/blackshirt.png,但是在浏览器的Console中查看时,路径变为http://localhost:3000/api/img/shirts/blackshirt.png

有什么办法可以改变吗?正如路径所建议的那样,我在 wwwroot 中有我的图像。从 Razor 视图:

<img class="img-fluid" src=@product.ImageUrl alt="shirt">

服务器未添加任何内容。浏览器开发工具会,因为它会将您的图像路径视为相对于当前请求 URL。在 URL 之前插入正斜杠,使其相对于网站的根目录:

<img class="img-fluid" src="/@product.ImageUrl" alt="shirt">