13

I have an MVC 3 / .NET 4.0 application running on Visual Studio 2012.

I just created a static bundle for all my JS and CSS files.

It first squawked at me for using paths without "~/" at the beginning, but I need to include files that are in another virtual directory on my server, so I used /../ to get them.

My bundle looks like this:

Bundle css = new Bundle("~/MyCSS", typeof(CssMinify));
css.AddFile("~/Content/css/Site.min.css");   
css.AddFile("~/../CommonWeb/css/fontawesome/css/font-awesome.css");   
BundleTable.Bundles.Add(css);

I'm trying to include font-awesome. It finds the CSS file just fine, but none of the fonts and icons are coming in. I'm guessing it is lines like this that cause the problem:

src: url('../font/fontawesome-webfont.eot');  /* From Font-Awesome */

Any thoughts on how to fix this? Thanks!

EDIT: More information:

When looking at the request for a relative CSS path, it goes after the root of my server:

http://localhost/font/fontawesome-webfont.woff

Instead of

http://localhost/CommonWeb/css/fontawesome/font/fontawesome-webfont.woff
Hao Kung
  • 27,364
  • 6
  • 81
  • 93
IronicMuffin
  • 4,154
  • 11
  • 45
  • 86
  • Did you ever get the bundled CSS to find the .EOT or .WOFF font files? I'm not able to. – flipdoubt Nov 17 '12 at 21:08
  • No, unfortunately. I just continued to link directly to the file outside of the bundle. – IronicMuffin Nov 19 '12 at 12:56
  • 1
    @IronicMuffin Have you seen this answer? Worked for me. Get the virtual bundle path to imitate the physical CSS path. http://stackoverflow.com/questions/12577108/asp-net-mvc4-bundling-with-twitter-bootstrap – Tom Troughton Nov 30 '12 at 13:59
  • 1
    Found a solution to this problem on [this question](http://stackoverflow.com/q/11355935/684831) – Dylan Meador Feb 10 '14 at 21:51

5 Answers5

3

CSS is parsed by the browser. URL's within CSS are relative to the CSS file's location. Not to the HTML page.

So just check where your fonts are uploaded, and they should work fine.

Aidiakapi
  • 5,590
  • 3
  • 30
  • 58
  • I added more information to show what's going on. The files are all in the right place, and when un-bundled, it works fine. – IronicMuffin Sep 27 '12 at 13:03
  • Where is the location of you CSS file? And where is the location of the font? – Aidiakapi Sep 27 '12 at 13:08
  • CSS and fonts are in a neighboring CSS directory. It looks like the Bundle is making the CSS file think it's at `"~/MyCSS"` so it is looking for the fonts folder at `../Fonts`, which in this case is the root... I'm guessing this won't work unless the bundler learns to pull in any linked files, too. – IronicMuffin Sep 27 '12 at 13:14
  • I mean the actual location in the resulting HTML file. (View source.) – Aidiakapi Sep 27 '12 at 13:21
  • The bundled file is linked to with the following: `` based on the bundle definition I showed above. If you don't see a way to get the images recognized easily when bundled, it's ok. I will just keep CSS files with peripheral files out of the bundle. – IronicMuffin Sep 27 '12 at 13:33
  • That's the problem... The bundled CSS isn't at the location of the original CSS files, therefore invalidating the urls. I recommend not bundling them, and the problem should be resolved. – Aidiakapi Sep 27 '12 at 13:36
  • 1
    Another option would be to create a bundle in the CommonWeb app, and reference that. – BNL Sep 27 '12 at 13:40
  • @Aidiakapi, agreed. That's the route I'm taking. – IronicMuffin Sep 27 '12 at 14:01
3

This would be going to the directory above the relative root of the web application:

css.AddFile("~/../CommonWeb/css/fontawesome/css/font-awesome.css");

Instead of doing this, copy that CSS file in a post build event for the project it exists in to the Content/css directory of the MVC project, using the macro syntax to make the COPY command easier. You can find that syntax here. Visual Studio also provides a mechanism for expanding the dialog and giving you assistance because it shows the actual path.

Finally, change the bundle to use:

css.AddFile("~/Content/css/font-awesome.css");
Mike Perrenoud
  • 63,395
  • 23
  • 143
  • 222
  • It finds the CSS file fine. I was trying to avoid copying the file to my project, as these are common files that are updated every so often. Even with a post build event, I can't just swap in a new version of the CSS without having to copy it to the other project's Content folder. For now I've just left it out of my bundle and linked it directly. – IronicMuffin Sep 27 '12 at 13:05
2

This SO post has a useful solution to this issue, and it appears to have been written by someone who actually works for Microsoft on the ASP.net Bundle code. (And weirdly, who edited this question, but didn't post an answer!)

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()));

This fixed my issue with getting 404 errors on Font Awesome icons, on the production server, due to the relative paths not being used correctly.

bundles.Add(new StyleBundle("~/Content/font-awesome/css/bundle").Include(
      "~/Content/font-awesome/css/font-awesome.css", new CssRewriteUrlTransform()));
Community
  • 1
  • 1
Ted
  • 2,315
  • 1
  • 27
  • 52
0

I had this issue with Django, If you are willing to use CDN you won't face this issue.

Just add this

<link rel="stylesheet" href="//maxcdn.bootstrapcdn.com/font-awesome/4.3.0/css/font-awesome.min.css">

src: url('../font') part will be resolved winthin the level of CDN than your server.

nehem
  • 9,162
  • 6
  • 44
  • 71
0

I was bundling little differently than yours, but the your edit on CSS relative path painfully looks like the same problem.This is how I was bundling.

 bundle.Add(new StyleBundle("~/Content/css").Include("~/Content/font-awesome.css", new CssRewriteUrlTransform()));

new CssReWriteUrlTransform() was transforming @font-face at css src: url('../fonts/fontawesome') to src: url('/fonts/fontawesome')

I took out new CssReWriteUrlTransform() and fonts were loading fine.

Also, make sure the fonts paths are relative to CSS file. If you are hosting in II8, make sure you have added .woff2 and .woff MIME type.

DevelopZen
  • 345
  • 2
  • 6