简体   繁体   English

MVC4捆绑错误的背景图片网址

[英]MVC4 Bundling misdirect background-image url

I have this bundling configuration: 我有以下捆绑配置:

bundles.Add(new StyleBundle("~/styles/style1").Include("~/Content/library/styles/style1.css")

Then I added this code to render the bundled CSS: 然后,我添加了以下代码以渲染捆绑的CSS:

@Styles.Render("~/styles/style1")

My CSS has this content: 我的CSS具有以下内容:

.style1 {
  background-image: url("../img/image.png");
}

Due to bundling, the path of background image is misdirected to ~/Content/library/img/image.png instead of ~/img/image.png. 由于捆绑,背景图片的路径被错误地定向到〜/ Content / library / img / image.png而不是〜/ img / image.png。 I don't want to edit the CSS file path because many other pages are using it. 我不想编辑CSS文件路径,因为许多其他页面正在使用它。 Do you know of any solution to it or am I missing a configuration in bundling? 您是否知道解决方案,还是我在捆绑中缺少配置?

You're gonna need to apply the CssRewriteUrlTransform to fix this: 您将需要应用CssRewriteUrlTransform来解决此问题:

bundles.Add(new StyleBundle("~/styles/style1")
                .Include("~/Content/styles/style1", new CssRewriteUrlTransform())

Alternatively, you can also use absolute paths in your stylesheets. 另外,您也可以在样式表中使用绝对路径。

PS: As stated in the comments, you have to add the Web Optimization Package to your project through Codeplex or NuGet to be able to use the CssRewriteUrlTransform class PS:如评论中所述,您必须通过Codeplex或NuGet将Web Optimization Package添加到项目中,才能使用CssRewriteUrlTransform类

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM