ASP.NET Bundles未加载字体文件

Zaf*_*far 49 c# asp.net asp.net-mvc bundle font-face

在我的ASP.NET MVC应用程序中,我使用Bundles来压缩css和js文件.问题是 - 启用优化模式后,字体未加载.

BundleTable.EnableOptimizations = true;
Run Code Online (Sandbox Code Playgroud)

这是C#代码

public static void RegisterBundles(BundleCollection bundles) {
    RegisterStyles(bundles);
    BundleTable.EnableOptimizations = true;
}

private static void RegisterStyles(BundleCollection bundles) {
bundles.Add(new StyleBundle("~/BundleStyles/css").Include(
    "~/Content/Styles/bootstrap/bootstrap.css",
    "~/Content/Styles/reset.css",
    "~/Content/Styles/gridpack/gridpack.css",

    "~/Content/Styles/fontFaces.css",
    "~/Content/Styles/icons.css",

    "~/Content/Styles/inputs.css",
    "~/Content/Styles/common.css",
    "~/Content/Styles/header.css",
    "~/Content/Styles/footer.css",
    "~/Content/Styles/cslider/slider-animations.css",
    "~/Content/Styles/cslider/slider-base.css"));
}
Run Code Online (Sandbox Code Playgroud)

这是字体的CSS.

   @font-face {
      font-family: ProximaNova;
      src: url('../Fonts/ProximaNova/ProximaNova-Bold.otf') format('opentype');
      font-weight: bold;
      font-style: normal;
    }
Run Code Online (Sandbox Code Playgroud)

以下是页面中引用CSS的方式.

<link href="/BundleStyles/css?v=pANk2exqBfQj5bGLJtVBW3Nf2cXFdq5J3hj5dsVW3u01" rel="stylesheet"/>
Run Code Online (Sandbox Code Playgroud)

但是,当我使用Chrome调试工具检查时,字体文件没有加载到页面,而我的页面看起来很糟糕,字体错误.

我究竟做错了什么?

Mah*_*him 39

好吧,我认为问题出在你的字体位置上.我假设捆绑的css虚拟位置/BundleStyles/css实际上并不存在.如果你的文件夹结构如下

内容>字体

内容>风格

如果这是真的,那就试试吧

改变/BundleStyles/css/Content/css

<link href="/Content/css?v=pANk2exqBfQj5bGLJtVBW3Nf2cXFdq5J3hj5dsVW3u01" rel="stylesheet"/>
Run Code Online (Sandbox Code Playgroud)

并像这样引用你的字体

src: url('Fonts/ProximaNova/ProximaNova-Bold.otf')
Run Code Online (Sandbox Code Playgroud)

在这种情况下,您的字体将相对于"css"文件加载,该文件位于内容文件夹中,该文件夹还包含"fonts"文件夹

如果我认为不正确,请告诉我们您的文件结构

  • 我有同样的问题来访问字体真棒字体,对于**其他解决方案**尝试这些链接处理`StyleBundle`**虚拟路径**:[链接1](http://www.mvccentral.net/story/详情/文章/ kahanu/stylebundle-403-error-resolved),[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 /),希望这有助于某人. (5认同)
  • 部署到IIS时,您可能会发现这很有用,http://codingstill.com/2013/01/set-mime-types-for-web-fonts-in-iis/ (4认同)
  • 我的答案有问题:它在调试模式下不起作用,因为字体将在样式文件夹下面引用,所以这就是我解决它的方法:我有一个Content/css文件夹和一个Content/fonts文件夹.我将bundle虚拟路径设置为"〜/ Content/css/styles",这样我就不必从字体引用中删除"../",因为fonts文件夹将保持在"styles"文件之上一级.也就是说,它在调试模式下保持正常工作,因为路径深度保持不变. (2认同)

小智 30

我认为CssRewriteUrlTransform可能是要走的路:

https://msdn.microsoft.com/en-us/library/system.web.optimization.cssrewriteurltransform(v=vs.110).aspx

像这样使用:

.Include("~/Content/bootstrap-cosmo.min.css", new CssRewriteUrlTransform())
Run Code Online (Sandbox Code Playgroud)

为我工作.


Mat*_*att 7

上面的答案很棒.

另一种选择 - 如果出于某种原因,上述内容对你不起作用 - 将改变@ font-face src属性引用'Fonts'文件夹的方式.'../'-ing不能很好地捆绑所以直接从站点根文件夹引用.确认'Fonts'文件夹是从根目录下来的一个,改变这个:

@font-face {
  src: url('../Fonts/ProximaNova/ProximaNova-Bold.otf') format('opentype');
}
Run Code Online (Sandbox Code Playgroud)

对此:

@font-face {
  src: url('/Fonts/ProximaNova/ProximaNova-Bold.otf') format('opentype');
}
Run Code Online (Sandbox Code Playgroud)

当站点在调试模式下运行时,您将检索相同的结果.

  • 如果您的应用程序部署为虚拟应用程序,它不起作用 (3认同)