在我的ASP.NET MVC应用程序,我正在使用Bundle 包压缩css和js文件.问题是,启用优化模式后字体没有加载.

BundleTable.EnableOptimizations = true;

下面是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"));
}

这里是字体的css.

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

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

<link href="/BundleStyles/css?v=pANk2exqBfQj5bGLJtVBW3Nf2cXFdq5J3hj5dsVW3u01" rel="stylesheet"/>

然而,当我使用Chrome调试器工具进行判断时,字体文件没有加载到页面,并且我的页面因字体错误而看起来很糟糕.

我做错了什么?

推荐答案

嗯,我认为问题在于你的字体位置.我假设Bundle 的css虚拟位置/BundleStyles/css实际上并不存在.如果你的文件夹 struct 如下

Content > Font

Content > style

如果这是真的,那么试试这个

/BundleStyles/css换成/Content/css

<link href="/Content/css?v=pANk2exqBfQj5bGLJtVBW3Nf2cXFdq5J3hj5dsVW3u01" rel="stylesheet"/>

然后像这样引用你的字体

src: url('Fonts/ProximaNova/ProximaNova-Bold.otf')

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

如果我的假设不正确,请告诉我们您是如何构建文件的

Asp.net相关问答推荐

如何从 Azure 上托管的应用服务获取登录用户名?

如何正确配置 IHttpModule?

取消选中时 ASP.NET CheckBox 不会触发 CheckedChanged 事件

使用 Owin 身份验证的服务器端声明缓存

如何从 web.config 中读取系统值并在 ASP.NET MVC C# 方法中使用

使用 JavaScript 禁用 ASP.NET 验证器

使用域用户的 SQL 连接字符串?

stream.CopyTo - 文件为空.网

如何使用自动生成的列隐藏 ASP.NET GridView 中的列?

您如何以编程方式填写表格并发布网页?

使用 ASPNet_Regiis 加密自定义配置部分 - 你能做到吗?

Page.IsValid 是如何工作的?

实体框架:如何解决外键约束可能导致循环或多个级联路径?

*.csproj 中的 usevshostingprocess 是什么?

不同域的登录页面

ASP.NET 5、EF 7 和 SQLite - SQLite 错误 1:没有这样的表:博客

Ashx 文件中的 HttpContext.Current.Session 为空

无法使用 ip:port 访问 WEB API,但可以在 VS 调试模式下使用 localhost:port

MVC4 中 Global.asax.cs 页面中的问题

在没有等待 #2 的情况下调用异步方法