【发布时间】:2015-08-07 11:21:35
【问题描述】:
我在 MVC 4 应用程序中遇到了 CSS 路径问题。例如,当我在开发环境中时,指向 imgs 的路径可以正常工作。但是当应用程序发布时,路径不起作用,导致 URL 无效。 我正在使用捆绑包,并且我的配置设置为发布,这意味着“调试模式=关闭”。
我有以下物理路径:
Content\admin\layout\css\themes
Content\admin\layout\img
在我的 CSS 中,我有几个图像映射如下:
background-image: url(../../img/sidebar_toggler_icon_darkblue.png);
映射到上面的“\img”路径。
在 Asp.net MVC 4 中进行正确路径映射的最佳选择是什么? 看了很多资源,但是都是在css/js这个标签上说话的。 我的问题是 CSS 内部的路径。
编辑 1
我已经在 Chrome 中看到了使用“F12”生成的 URL。它确实指向:
MyServer/img/sidebar_inline_toggler_icon_darkblue.jpg
在本地运行时它指向:
localhost:50390/Content/admin/layout/img/sidebar_toggler_icon_darkblue.png
我知道重写所有 css 路径应该可行,但我怀疑这是正确的做法。我一定是错过了什么。
我的包配置为:
bundles.Add(new StyleBundle("~/css").Include(
"~/Content/themes/base/jquery.ui.dialog.css",
"~/Content/themes/base/jquery.ui.theme.css",
"~/Content/global/plugins/font-awesome/css/font-awesome.css",
"~/Content/global/plugins/simple-line-icons/simple-line-icons.css",
"~/Content/global/plugins/bootstrap/css/bootstrap.css",
"~/Content/global/plugins/bootstrap-switch/css/bootstrap-switch.css",
"~/Content/global/css/components.css",
"~/Content/global/css/plugins.css",
"~/Content/global/plugins/datatables/plugins/bootstrap/dataTables.bootstrap.css",
"~/Content/admin/layout/css/layout.css",
"~/Content/admin/layout/css/themes/darkblue.css",
"~/Content/admin/layout/css/custom.css",
"~/Content/themes/base/jquery.ui.datepicker.css"
));
已经看过这些链接,但找不到路:
CSS and Javascript relative path confusion in ASP.NET MVC
CSS/JS bundle in single file in mvc when publish with release option
【问题讨论】:
-
CSS 中的路径是相对于 CSS 文档的。所以检查那里的开始。使用 Chrome 开发者工具 (f12) 等工具检查发布的结果,以确保路径符合您的预期。最后,如果可能的话,如果实际使用根相对路径
/Content/admin/layout/img/sidebar_toggler_icon_darkblue.png(即您的开发和生产环境都解析为http://asiteroot.com/Content/admin/layout/img/sidebar_toggler_icon_darkblue.png
标签: css asp.net-mvc asp.net-mvc-4 relative-path