ASP.NET MVC4 Bundling with Twitter Bootstrap
G

4

37

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?

Gora answered 25/9, 2012 at 6:12 Comment(0)
L
38

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()));
Lao answered 25/9, 2012 at 16:31 Comment(8)
hao-kung, this doesn't seem to work for me with font-face rules. Should it?Gradin
@Gradin can you post an example/repro with what doesn't work?Lao
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?Gradin
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?Gradin
Yep, best place is to file an issue here: aspnetoptimization.codeplex.com/workitem/list/advancedLao
@HaoKung: The virtual directory issue has been reported here: aspnetoptimization.codeplex.com/workitem/83 but it looks like it hasn't been addressed yet.Pile
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.Mountainous
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 problemPeduncle
E
28

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);           
    }
}
Elk answered 17/7, 2013 at 14:38 Comment(3)
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.Abracadabra
this is the answer when using virtual directory, worked for meImtiaz
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/AspNetBundlingOrme
D
5

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.

Dual answered 25/9, 2012 at 15:34 Comment(0)
O
1

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
Orme answered 15/12, 2015 at 11:14 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.