ASP.NET MVC4 Bundling with Twitter Bootstrap

22,370

Solution 1

The issue is most likely that the icons/images in the css files are using relative paths, so if your bundle doesn't live in the same app relative path as your unbundled css files, they become broken links.

We have rebasing urls in css on our todo list, but for now, the easist thing to do is to have your bundle path look like the css directory so the relative urls just work, i.e:

new StyleBundle("~/Static/Css/bootstrap/bundle")

Update: We have added support for this in the 1.1beta1 release, so to automatically rewrite the image urls, you can add a new ItemTransform which does this rebasing automatically.

bundles.Add(new StyleBundle("~/bundles/publiccss").Include(
            "~/Static/Css/bootstrap/bootstrap.css",
            "~/Static/Css/bootstrap/bootstrap-padding-top.css",
            "~/Static/Css/bootstrap/bootstrap-responsive.css",
            "~/Static/Css/bootstrap/docs.css", new CssRewriteUrlTransform()));

Solution 2

The 'CssRewriteUrlTransform' works just fine for applications that DON'T run on top of a virtual directory.

So, if your app runs on http://your-site.com/ it runs just fine, but if it runs on http://your-site.com/your-app/ you'll have 404 for all your images because the default 'CssFixRewriteUrlTransform' is referencing your images with a '/'.

To solve this issue, I have implemented my own version of 'CssRewriteUrlTransform' like this:

    public class CssFixRewriteUrlTransform : IItemTransform {
    private static string ConvertUrlsToAbsolute(string baseUrl, string content) {
        if (string.IsNullOrWhiteSpace(content)) {
            return content;
        }
        var regex = new Regex("url\\(['\"]?(?<url>[^)]+?)['\"]?\\)");
        return regex.Replace(content, match => string.Concat("url(", RebaseUrlToAbsolute(baseUrl, match.Groups["url"].Value), ")"));
    }

    public string Process(string includedVirtualPath, string input) {
        if (includedVirtualPath == null) {
            throw new ArgumentNullException("includedVirtualPath");
        }
        var directory = VirtualPathUtility.GetDirectory(includedVirtualPath);
        return ConvertUrlsToAbsolute(directory, input);
    }

    private static string RebaseUrlToAbsolute(string baseUrl, string url) {
        if (string.IsNullOrWhiteSpace(url) || string.IsNullOrWhiteSpace(baseUrl) || url.StartsWith("/", StringComparison.OrdinalIgnoreCase)) {
            return url;
        }
        if (!baseUrl.EndsWith("/", StringComparison.OrdinalIgnoreCase)) {
            baseUrl = string.Concat(baseUrl, "/");
        }
        return VirtualPathUtility.ToAbsolute(string.Concat(baseUrl, url));
    }
}

UPDATE: thanks to superjos who pointed out that there was another solution out there:

public class CssRewriteUrlTransformWrapper : IItemTransform
{
    public string Process(string includedVirtualPath, string input)
    {           
        return new CssRewriteUrlTransform().Process("~" + VirtualPathUtility.ToAbsolute(includedVirtualPath), input);           
    }
}

Solution 3

What you can do is you can go to the customize page and change @iconSpritePath and @iconWhiteSpritePath in the Sprites section and, of course, download the new style.

I've put my images in the folder Content/Images folder and I've changed the path in:

  • /Content/Images/glyphicons-halflings.png
  • /Content/Images/glyphicons-halflings-white.png

Another alternative is to download all the LESS files from github, change the same variables in the variables.less file and recompile the bootrap.less file with a tool like SimpLESS.

Solution 4

Fix for this now added to my AspNetBundling NuGet package which resolves a bunch of other issues in the standard transformer, particularly around using data-uris. Open-sourced on GitHub too.

Just do:

  1. Install-Package AspNetBundling
  2. Replace CssRewriteUrlTransform with CssRewriteUrlTransformFixed
Share:
22,370
Pelle
Author by

Pelle

a 'senior' developer/architect with a passion for agile practices

Updated on December 16, 2020

Comments

  • Pelle
    Pelle over 3 years

    I'm trying to use the new bundling feature in MVC 4 with Twitter bootstrap and it seems to me like the paths to the glyphicons png-files int the css get's messed up in some way. Heres my code:

     bundles.Add(new StyleBundle("~/bundles/publiccss").Include(
                "~/Static/Css/bootstrap/bootstrap.css",
                "~/Static/Css/bootstrap/bootstrap-padding-top.css",
                "~/Static/Css/bootstrap/bootstrap-responsive.css",
                "~/Static/Css/bootstrap/docs.css"));
    
    
            bundles.Add(new ScriptBundle("~/bundles/publicjs").Include(
                "~/Static/Js/jquery-1.7.2.js",
                "~/Static/Js/bootstrap/bootstrap.js",
                "~/Static/Js/cookie/jquery.cookie.js"));
    

    I'm not seeing any icons on buttons and likewise. Am I doing something wrong here? Any suggestions?

  • flipdoubt
    flipdoubt over 11 years
    hao-kung, this doesn't seem to work for me with font-face rules. Should it?
  • Hao Kung
    Hao Kung over 11 years
    @flipdoubt can you post an example/repro with what doesn't work?
  • flipdoubt
    flipdoubt over 11 years
    Since this isn't a web optimization tech support forum, should I post it to a particular Microsoft forum that allows me to upload the repro or something?
  • flipdoubt
    flipdoubt over 11 years
    Does it help to know it is a .NET 4.0 project rather than a .NET 4.5 project in IIS Express rather than full-blown IIS?
  • Hao Kung
    Hao Kung over 11 years
    Yep, best place is to file an issue here: aspnetoptimization.codeplex.com/workitem/list/advanced
  • superjos
    superjos over 10 years
    In the comments to this codeplex workitem, user MadBender on Aug 9 at 10:09 AM suggested what it seems a simpler workaround to this problem.
  • Charles Burns
    Charles Burns over 8 years
    @HaoKung: The virtual directory issue has been reported here: aspnetoptimization.codeplex.com/workitem/83 but it looks like it hasn't been addressed yet.
  • Jack M
    Jack M over 8 years
    this is the answer when using virtual directory, worked for me
  • benmccallum
    benmccallum over 8 years
    Fix now added to my CssRewriteUrlTransformFixed in a NuGet package I have up which fixes a bunch of issues in the standard transformer. github.com/benmccallum/AspNetBundling
  • Shaiju T
    Shaiju T about 8 years
    i had the same issue to access font awesome fonts, for other solutions try these links which deals with StyleBundle virtualpath: Link 1 , Link 2, Link 3, Link 4 , hope this helps someone.
  • Nick.McDermaid
    Nick.McDermaid about 7 years
    This did not work for me. Maybe I don't understand it well enough but it seems like bundling is far more trouble than it's worth. Does CssRewriteUrlTransform affect .min. files? I suspect that is my problem