ASP.NET核心中的相对路径
我正在尝试将本地图像插入我的CSHTML文件。该图像位于与可执行文件的同一文件夹中。我已经尝试了以下操作,但是这些选项都不可用:
<img src="image.png" alt="Image" />
<img src="~/image.png" alt="Image" />
<img src="@Url.Content("~/image.png")" alt="Image" />
但是,绝对路径和外部URL非常有效:
<img src="C:/fake/path/image.png" alt="Image" />
<img src="www.example.com/image.png" alt="Image" />
这是在CMD中执行应用程序的方式,我尝试将同一映像复制到c:
, c:/api
and c:/api/win-x64
,以防万一仍然没有(c:/api/win-中没有子文件夹x64
)。
有人知道为什么会发生这种情况吗?我不想使用绝对路径。
I'm trying to insert a local image in my CSHTML file. The image is located in the same folder as the executable. I have tried the following, but none of these options work:
<img src="image.png" alt="Image" />
<img src="~/image.png" alt="Image" />
<img src="@Url.Content("~/image.png")" alt="Image" />
However, absolute paths and external URLs work perfectly:
<img src="C:/fake/path/image.png" alt="Image" />
<img src="www.example.com/image.png" alt="Image" />
This is how executing the app looks in cmd, I tried copying the same image to C:
, C:/API
and C:/API/win-x64
, just in case, still nothing (there are no subfolders in C:/API/win-x64
).
Does anyone know why is this happening? I don't want to use absolute paths.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
要访问 Asp.net Core 中的静态文件,必须将它们放在 wwwroot 文件夹中
根据上图,图片链接如下
更多信息:
https://learn.microsoft.com/ en-us/aspnet/core/fundamentals/static-files?view=aspnetcore-6.0
To access static files in the Asp.net core, you must place them in the wwwroot folder
According to the image above, the image link is as follows
more information :
https://learn.microsoft.com/en-us/aspnet/core/fundamentals/static-files?view=aspnetcore-6.0