"woff"、"woff2" 适用于本地主机,但不适用于 Web 服务器并出现控制台错误

"woff", "woff2" works on localhost but not on web servers and giving console errors

我在 ASP.NET MVC 环境中使用超棒的字体,以及带有 "woff"、"woff2" 扩展的大多数字体系列。他们在本地主机上工作得非常好。

但是当涉及到部署环境时,大多数时候我们无法像本地主机那样获得实际的字体系列样式。

我们可以做些什么来避免这个问题?

只需定义字体文件扩展名,将此代码添加到 web.config 文件

<system.webServer>
  <staticContent>
     <mimeMap fileExtension="woff" mimeType="application/font-woff" />
     <mimeMap fileExtension="woff2" mimeType="application/font-woff" />
  </staticContent>
</system.webServer>

您应该在部署服务器上注册字体的 MIME 类型,使用 web.config 这样做:

<system.webServer>
    <staticContent>
      <remove fileExtension=".woff2" />
      <mimeMap fileExtension=".woff2" mimeType="application/x-font-woff" />
      <remove fileExtension=".woff" />
      <mimeMap fileExtension=".woff" mimeType="application/x-font-woff" />
    </staticContent>
</system.webServer>

注意:我添加了一个删除标签只是为了确定,因为如果类型被注册它会抛出一个错误。 此外,如果您使用的是 MVC,请检查您的资源包,因为部署发布应用程序会弄乱资源的路径。