4

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:

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

My CSS has this content:

.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. I don't want to edit the CSS file path because many other pages are using it. Do you know of any solution to it or am I missing a configuration in bundling?

rajeemcariazo
  • 2,476
  • 5
  • 36
  • 62
  • ~/Content/styles/style1 style1 is your css file? then you should use it so ~/Content/styles/style1.css – xurca Aug 30 '13 at 10:44
  • @xurca edited my question now, but i hope you got my question, the main concept is that bundling is misdirecting url paths – rajeemcariazo Aug 30 '13 at 10:47
  • well i can suggest you to check this link: http://stackoverflow.com/questions/11355935/mvc4-stylebundle-not-resolving-images – xurca Aug 30 '13 at 10:55

1 Answers1

11

You're gonna need to apply the CssRewriteUrlTransform to fix this:

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

Alternatively, you can also use absolute paths in your stylesheets.

P.S: 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

haim770
  • 48,394
  • 7
  • 105
  • 133
  • is CssRewriteUrlTransform class builtin in VS2012? – rajeemcariazo Aug 30 '13 at 10:53
  • Try to add a reference to `System.Web.Optimization`. if you can't find it, look here: http://aspnetoptimization.codeplex.com – haim770 Aug 30 '13 at 10:57
  • 1
    Or, alternatively, use NuGet: `Install-Package Microsoft.AspNet.Web.Optimization` – Patryk Ćwiek Aug 30 '13 at 11:01
  • Beware of issues with CssRewriteUrlTransform, like data url's and virtual directories, and consider using [CssRewriteUrlTransformFixed.cs](https://github.com/benmccallum/AspNetBundling/blob/master/AspNetBundling/CssRewriteUrlTransformFixed.cs), instead. – R. Schreurs May 01 '17 at 15:12