54

If I direct refer to font-awesome.css in _layouts page. it will work

<link href="~/Content/font-awesome-4.0.3/css/font-awesome.css" rel="stylesheet" />

But I used in BundleConfig.cs. Icon is not showing.

 bundles.Add(new StyleBundle("~/Content/css").Include(
                        "~/Content/font-awesome-4.0.3/css/font-awesome.css",
                        "~/Content/bootstrap.css",                        
                         "~/Content/body.css",
                        "~/Content/site.css",
                        "~/Content/form.css"
                     ));

and also I observed browser console have error to font directory. Failed to load resource: the server responded with a status of 404 (Not Found) http://localhost:51130/fonts/fontawesome-webfont.woff?v=4.0.3

what could be the problem?

James123
  • 11,184
  • 66
  • 189
  • 343

6 Answers6

113

Try using CssRewriteUrlTransform when bundling:

bundles.Add(new StyleBundle("~/Content/css").Include(
        "~/Content/bootstrap.css",                        
        "~/Content/body.css",
        "~/Content/site.css",
        "~/Content/form.css"
    ).Include("~/Content/font-awesome-4.0.3/css/font-awesome.css", new CssRewriteUrlTransform());

This changes any urls for assets from within the css file to absolute urls so the bundling doesn't mess up the relative path.

Docs for CssRewriteUrlTransform

Simon C
  • 9,458
  • 3
  • 36
  • 55
  • 8
    Make sure that you either fix paths inside font-awesome.min.css as well (or just remove this file, as bundling will minify your css anyway). Otherwise it might pickup the .min.css file and transforms will not happen. – miha Dec 11 '14 at 17:10
  • 19
    In my case I had to delete the .min.css version -- it was causing the optimizer to not generate a minified version with the correct path – feradz Feb 22 '15 at 14:35
  • 1
    i had the same issue to access font awesome fonts, for **other solutions** try these links which deals with `StyleBundle` **virtualpath**: [Link 1](http://www.mvccentral.net/story/details/articles/kahanu/stylebundle-403-error-solved) , [Link 2](http://forums.asp.net/t/1774324.aspx?MVC4+css+bundling+and+image+references), [Link 3](http://ericpanorel.net/2013/10/25/font-awesome-4-0-mvc-bundling-and-minification/), [Link 4](http://jameschambers.com/2014/08/adding-some-font-awesome-to-mvc-and-bootstrap/) , hope this helps someone. – Shaiju T Feb 25 '16 at 09:37
  • 3
    If your application is hosted inside a virtual directory in IIS, you'll have to override `CssRewriteUrlTransform` to take this into account. You can find an example of that here on stackoverflow, just google "CssRewriteUrlTransform virtual directory". – Rudey Apr 20 '16 at 17:49
  • 1
    I have also noted that when you add a bundle, it should not coincide with some real path of your site. e.g if you are creating a bundle like so `bundles.Add(new StyleBundle("~/Content/fontawesome").Include("~/Content/font-awesome/css/font-awesome.css"))` then there should not be actual font-awesome folder present under your site's Content folder, otherwise it will give you a runtime weird error. – Faisal Mq Oct 31 '16 at 11:29
  • Include("~/~/~/font-awesome.css", new CssRewriteUrlTransform())); Don't forget the last ). – Sh7ne Apr 23 '18 at 19:43
11

My solution was simple: run PM> Install-Package FontAwesome, and reference the correct path:

.Include("~/Content/font-awesome.min.css")
Sith2021
  • 3,245
  • 30
  • 22
  • 2
    This. I'm using FontAwesome 4.2 from NuGet, and this is all it took (although it makes no sense to reference the ".min" version of the file, since bundling will take care of minification for you). – Gary McGill Sep 03 '15 at 23:58
  • Thanks!! That fixed my issue! – Fermín Nov 05 '16 at 13:26
3

I had the same error message and fixed after setting mime types for web fonts in IIS .

MarwaAhmad
  • 808
  • 9
  • 21
  • I followed the article and removing mime-types and re-including in web config solved the problem. Also I had to move the font-awesome.min.css file under `/Content` directory and moved the fonts under `/fonts`. – nim_10 Jun 14 '17 at 06:27
2

With version 5.1.0 I had to reference all.css instead of fontawesome.css i.e.,

bundles.Add(new StyleBundle("~/bundles/fontawesome").Include(
    "~/Content/all.css",
    new CssRewriteUrlTransform()
));
ZerosAndOnes
  • 1,083
  • 1
  • 13
  • 25
1

I also had this same error message. I had to do a combination of the answers listed in this thread:

1/ Add this line of code as suggested by @Simon C:

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

This worked to fix the relative urls, however, I had to delete the font-awesome.min.css file from my bower directory every time I published otherwise the bundler would get confused and not include and minify the font-awesome.css file. So...

2/ I had to do what @miha suggested in a comment and fix the relative urls in the font-awesome.min.css file with CssRewriteUrlTransform(). So I decided to rewrite the urls in the .min file and just include that instead of the unminified version and it worked:

.Include("~/Content/font-awesome-4.0.3/css/font-awesome.min.css", new CssRewriteUrlTransform());

If I understand correctly, the bundler won't try to minify the .min file again because it's already minified. The only "drawback" is it does not concatenate the font-awesome.min.css content into the single css file that the bundler creates. It will be included separately.

AlexB
  • 7,302
  • 12
  • 56
  • 74
big_water
  • 3,024
  • 2
  • 26
  • 44
1

I add another answer to this question has I found the solution by mixing some of them.

  1. Most voted answer is the main solution, but it is also important to
  2. Add the entry suggested by this answer. Finally
  3. Follow the comment from @feradz in most voted answer: "delete the .min.css version -- it was causing the optimizer to not generate a minified version with the correct path"

Last point is the key of all: distributed "min" versions of the js files, does not follow the "CssRewriteUrlTransform" rules. So, manually deleting bootstrap.min.css, font-awesone.min.css definitively solved the issue.

Gianpiero
  • 3,349
  • 1
  • 29
  • 42