简体   繁体   English

如何正确地将来自 wwwroot 的图像提供给“页面”子文件夹中的剃刀页面?

[英]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.

相关问题 如何在 Razor 页面上创建指向存储在 wwwroot 中的图像的超链接? - How do I create a hyperlink on a Razor page to an image stored in wwwroot? ASP.Net剃刀页面-如何将JSON从剃刀页面返回到javascript $ .getJson? - ASP.Net Razor Pages - How do you return json from a razor page to javascript $.getJson? 如何在没有控制器的 Razor 页面中找到 wwwroot 的物理路径? - How can I find the physical path of wwwroot, in a Razor page without a controller? 如何在没有 WWWROOT 的情况下使用 ASP.NET 在我的 Web 应用程序中提供 html 文件? - How do I serve an html file in my Web App with ASP.NET without the WWWROOT? 在 .net 核心 razor 页面中 - 如何从父记录页面导航到子记录页面? - In .net core razor pages- how do you navigate from parent record page to a child record page? 如何从 .net 中 wwwroot 内的多个文件夹中提供 html 文件 - How to serve html files from multiple folders inside wwwroot in .net 如何从ASP.Net网页和剃须刀上使用MongoDB? - How do I work with MongoDB from ASP.Net web pages and razor? 如何在 Razor 页面 Not MVC 中填充下拉列表 - How do I populate dropdownlist inside Razor pages Not MVC 如何使用 razor 页面一次显示一个文本? - How do I display text one at a time using razor pages? 如何在剃须刀页面的属性内转义引号 - How do I escape quotes inside attribute in razor pages
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM