0

I know there are other questions, but none have the answer I need.

Just need to be able to force an absolute path in some LESS that's included in an MVC page so that the images (etc) are correctly picked up.

[websiteroot]
----Content
--------img
------------<image files>
--------bootstrap
------------<bootstrap files>
--------site.LESS
--------Site_styles.LESS
----Views
--------Shared
------------_Layout.cshtml

It's not even in a bundle, it's just directly included in _Layout.cshtml as

 @Styles.Render("~/Content/Site_styles.less");

In Site_styles.less I have

.carousel-image-1
{
    width: 100%;
    background: url('img/banner1.jpg') no-repeat;
}

but I can't get it to be absolute, i.e.

"~/Content/img/banner1.jpg"

it is rendered as

background: url('/Content/img/sodexo_banner1.jpg')

In case it matters, other LESS [bootstrap etc] is in a bundle like so

BundleTable.EnableOptimizations = false;

    bundles.UseCdn = true;
    var cssTransformer = new CssTransformer();
    var jsTransformer = new JsTransformer();
    var nullOrderer = new NullOrderer();

    var cssBundle = new StyleBundle("~/Content/css");
    cssBundle.Include("~/Content/Site.less");
    cssBundle.Include("~/Content/bootstrap/bootstrap.less");

    //cssBundle.Include("~/Content/Site_variables.less");
    cssBundle.Transforms.Add(cssTransformer);
    cssBundle.Orderer = nullOrderer;
    bundles.Add(cssBundle);
Colin Bacon
  • 15,436
  • 7
  • 52
  • 72
kpollock
  • 3,899
  • 9
  • 42
  • 61

1 Answers1

0

Use CssRewriteUrlTransform.

From docs:

Rewrites urls to be absolute so assets will still be found after bundling.

Example

bundles.Add(new StyleBundle("~/Content/css")
        .Include("~/Content/Site.less")
        .Include("~/Content/bootstrap/bootstrap.less", new CssRewriteUrlTransform()));
Colin Bacon
  • 15,436
  • 7
  • 52
  • 72
  • I did - and it made no difference - probably because my Site_styles.less script is **NOT** bundled. As mentioned above. But gimme a sec and I WILL include it in the bundle then try CssRewriteUrlTransform... – kpollock Oct 20 '14 at 10:52
  • Nope, putting in cssBundle.Include("~/Content/Site_styles.less", new CssRewriteUrlTransform()); still gives background: url('/Content/img/banner1.jpg') – kpollock Oct 20 '14 at 10:53