[英]How do I properly serve images from the wwwroot to razor pages in a “Page” subfolder?
I have a JavaScript function that creates a fading slideshow.我有一个创建渐变幻灯片的 JavaScript 函数。 It uses images in the directory:
它使用目录中的图像:
"wwwroot/images/backgrounds"
“wwwroot/图像/背景”
If I load a page that's directly inside the Pages folder, the images load properly.如果我加载一个直接位于Pages文件夹内的页面,图像会正确加载。 But if I load a page that's inside a subfolder, the browser tries to get the images from inside that folder instead of from the root folder.
但是,如果我加载位于子文件夹内的页面,浏览器会尝试从该文件夹内而不是从根文件夹中获取图像。
So instead of所以代替
"localhost/images/backgrounds/bg1.jpg"
“本地主机/图像/背景/bg1.jpg”
I get我得到
"localhost/subfolder in 'Pages' folder/images/backgrounds/bg1.jpg"
“'Pages'文件夹/images/backgrounds/bg1.jpg中的本地主机/子文件夹”
I'm still new to this, so I'm not sure why the wwwroot folder isn't serving the images properly, but I set my default page to one in the same subfolder and it works perfectly, so I'm at a loss at to what's wrong.我还是个新手,所以我不确定为什么 wwwroot 文件夹不能正确提供图像,但是我将默认页面设置为同一个子文件夹中的一个并且它运行良好,所以我不知所措出了什么问题。
Any ideas or solutions would be greatly appreciated at this point.在这一点上,任何想法或解决方案将不胜感激。
You're loading your images from a relative path - that is, the path that's exactly under the current directory .您正在从相对路径加载图像 - 即当前目录下的路径。 You want it to be based on an absolute path - that is, off of the same directory, no matter where the page is.
您希望它基于绝对路径——也就是说,无论页面在哪里,都在同一个目录之外。 To do that, use a slash in the beginning of your path:
/wwwroot/images/backgrounds
为此,请在路径开头使用斜杠:
/wwwroot/images/backgrounds
This is actually by design - imagine you had sub-directories in your web application that needed their own images.这实际上是设计使然 - 假设您的 Web 应用程序中有需要自己图像的子目录。 You can then load an image, like
images/pageBackground.png
that's relative to each page the user is on - very convenient!然后,您可以加载与用户所在的每个页面相关的图像,例如
images/pageBackground.png
- 非常方便! Just remember that leading slash - which means start this path from the virtual root of the application instead of the current sub-directory - and you'll get the right file!请记住前导斜杠 - 这意味着从应用程序的虚拟根目录而不是当前子目录开始此路径- 您将获得正确的文件!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.