Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.NET MVC4 Bundling with Twitter Bootstrap

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?

like image 298
Pelle Avatar asked Sep 25 '12 06:09

Pelle


Video Answer


2 Answers

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())); 
like image 184
Hao Kung Avatar answered Sep 24 '22 20:09

Hao Kung


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);                } } 
like image 29
Luís Deschamps Rudge Avatar answered Sep 22 '22 20:09

Luís Deschamps Rudge