图像未从主题布局中显示(Orchard CMS)
我正在尝试为 Orchard CMS 创建主题。我的模板不是为此制作的,因此在显示 Layout.cshtml 中的图像时遇到一些问题。
这是我的网络服务器上当前的文件夹结构(仅主题文件夹结构):
Theme/Content/Images/Image.jpg 主题/视图/Layout.cshtml Theme/Styles/Site.css
以下行不显示图像(位于 Layout.cshtml 中):
但是,此行确实显示图像(位于 Site.css 中):
background-image:url('../Content/Images/bgLines.png');
我相信问题是 Layout.cshtml 不显示来自 Theme/Views/Layout.cshtml 的图像,而是显示来自其他位置的图像。如果有人知道那个位置是什么或者如何覆盖它,我将不胜感激。
I'm trying to create theme for Orchard CMS. The template I have wasn't made for it so I have some troubles displaying images from Layout.cshtml.
This is the current folder structure on my web server (theme folder structure only):
Theme/Content/Images/Image.jpg
Theme/Views/Layout.cshtml
Theme/Styles/Site.css
The following line doesn't display image (located in Layout.cshtml):
<img src="../Content/Images/bgBig.jpg" alt="Big background image" />
However, this line does display the image (located in Site.css):
background-image:url('../Content/Images/bgLines.png');
I believe the problem is that Layout.cshtml doesn't display the image from Theme/Views/Layout.cshtml, but from the other location. If someone knows what would be that location or how to override it I would be thankful.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
我可能有点晚了,但这可能对其他人有帮助。
要获取当前主题,然后构建动态路径(而不是绝对路径),请使用:
WorkContext.CurrentTheme
:获取当前工作主题 ExtensionDescriptor。然后将其提供给 Html.ThemePath URL 生成器:
前任。
玩得开心!
此致,
蒂亚戈.
I might be a little late, but this may be of help to others.
To get the current theme and then build an dynamic path (as opposed to an absolute path) use this:
WorkContext.CurrentTheme
: Gets the current working theme ExtensionDescriptor.Then give it to the Html.ThemePath URL builder:
Ex.
Have fun!
Best regards,
Tiago.
在 Layout.cshtml 中添加图像时,您应该使用主题的完整路径(例如 /Themes/My.Theme/Content/Images/MyImage.jpg)。请记住,您在 [img] 标签中提供的路径相对于浏览器中的 URL,而不是服务器上的路径。在 MVC 中,这些几乎从来都是不平等的。
Layout.cshtml 视图文件作为每个请求的一部分加载,因此放置在其中的相对路径几乎总是会中断。
假设您有两个 Orchard 页面:site.com/mypage 和 site.com/something/mypage。 Layout.cshtml 在两者中都会呈现。当您访问第二个时,适用于第一个的相对 URL 肯定会中断。
CSS 样式表通过指定 /Themes/YourTheme/Styles 文件夹中的物理文件的绝对路径直接加载(默认) ,因此在这种情况下相对 URL 将起作用。
华泰
When adding images in Layout.cshtml you should use the full path to your theme (eg. /Themes/My.Theme/Content/Images/MyImage.jpg). Remember that the paths you provide in [img] tag are relative to the URL in browser, not the path on the server. In MVC those are almost never equal.
Layout.cshtml view file gets loaded as a part of every single request, so relative paths placed inside will almost always break.
Imagine you have two Orchard pages: site.com/mypage and site.com/something/mypage. Layout.cshtml gets rendered in both of them. Relative URLs working for the first will surely break when you access second one.
CSS stylesheets are loaded directly by specifying absolute path to the physical files in /Themes/YourTheme/Styles folder (default), so in this case relative URLs will work.
HTH
感谢蒂亚戈提供的解决方案。我认为这实际上是正确的解决方案,而不是链接完整路径,我认为这需要 Orchard 站点位于域的根目录上。
原始问题的完整图像参考如下所示:
Thx Tiago for your solution. I think this is in fact the correct solution, as opposed to linking the full path which I think would require the Orchard site to be on the root of the domain.
The full image reference of the original question would look like this:
我很惊讶没有人提到您在图像/脚本/样式所在的文件夹中需要以下
web.config
(请参阅果园文档)此外,正如其他人指出的那样,这是定位图像的最可靠方法:
I'm surprised that nobody's mentioned that you need the following
web.config
in the folder in which your images/scripts/styles reside (see the orchard docs)Additionally, as others have pointed out, this is the most reliable way of locating an image:
如果您检查源,它应该向您显示它试图在哪里找到该图像但失败了。这很可能是相对路径有问题,请尝试 css 中的绝对路径,看看是否是问题所在。没有实际的网站,我无法确定。
if you examine the source, it should show you where it's trying to find that image and failing. It's most likely the relative path it's having issue with, try an absolute path in the css to see if that's the issue. without the actual site, I can't know for sure.