c# - 字体文件未使用 ASP.NET 包加载

标签 c# asp.net asp.net-mvc bundle font-face

在我的 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 实际上并不存在。如果您的文件夹结构如下所示

内容 > 字体

内容 > 风格

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

/BundleStyles/css 更改为 /Content/css

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

并像这样引用你的字体

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

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

如果我的假设不正确,请向我们展示您的文件结构

关于c# - 字体文件未使用 ASP.NET 包加载,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19000083/

相关文章:

c# - 使用字典和泛型创建嵌套的 JSON 可序列化对象结构时,.NET 中的 C# 协方差存在问题

c# - 强制 XmlDefaultValue 值的 XML 序列化

c# - 遍历所有颜色?

c# - SendGrid 解析入站 header

c# - HttpPostedFileBase 总是返回 null 并且图像不会被发布

c# - 在 Mongodb 中将 JObject 存储为 C# 类的属性

asp.net - 使用 ASP.Net 2.0 创建 SOAP 请求

c# - 在 C# 中使用 TcpClient.Connect() 进行 WHOIS 查询是否昂贵?

asp.net-mvc - 在 MVC 和 WCF 应用程序中共享 ASP.Net 应用程序缓存

c# - UserAgent 字符串中的端口扫描器 - masscan