ASP.NET核心中的相对路径

发布于 2025-01-19 05:55:11 字数 760 浏览 1 评论 0原文

我正在尝试将本地图像插入我的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).

enter image description here

Does anyone know why is this happening? I don't want to use absolute paths.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

太阳公公是暖光 2025-01-26 05:55:11

要访问 Asp.net Core 中的静态文件,必须将它们放在 wwwroot 文件夹中

在此处输入图片描述

根据上图,图片链接如下

<img src="/uploadedFiles/images/img_sample_five.jpg"  />

更多信息:

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

enter image description here

According to the image above, the image link is as follows

<img src="/uploadedFiles/images/img_sample_five.jpg"  />

more information :

https://learn.microsoft.com/en-us/aspnet/core/fundamentals/static-files?view=aspnetcore-6.0

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文