【问题标题】:application/font-woff2 not working when using Asp.Net VNext使用 Asp.Net VNext 时应用程序/font-woff2 不起作用
【发布时间】:2015-04-29 01:58:05
【问题描述】:

我正在用 VNext + OSX + Chrome 做一些实验。我正在尝试获取 woff2 文件

GET http://localhost:5003/fonts/fontawesome-webfont.woff2?v=4.3.0 

但是发生了错误。请参阅下面的请求标头

Remote Address:127.0.0.1:5003
Request URL:http://localhost:5003/fonts/fontawesome-webfont.woff2?v=4.3.0
Request Method:GET
Status Code:404 Not Found

这是我的 Startup.cs

    public void Configure(IApplicationBuilder app)
    {
        app.UseStaticFiles();

        app.UseServices(services =>
        {
            services.AddMvc();
        });

        // Add MVC to the request pipeline
        app.UseMvc(routes =>
        {
            routes.MapRoute(
                name: "default",
                template: "{controller}/{action}/{id?}",
                defaults: new { controller = "Home", action = "Index" });
        });
    }

我在 Github 的 AspNet 项目中看到了有关 StaticFiles 的内容(链接如下),它似乎受到支持。

https://github.com/aspnet/StaticFiles/blob/dev/src/Microsoft.AspNet.StaticFiles/FileExtensionContentTypeProvider.cs

你们能帮帮我吗?

【问题讨论】:

  • @CharlesBurns 实际上,我的帖子不是专门针对 II8 的,其他解决方案也不起作用,因为我没有使用 Web.Config。它们不是同一个解决方案。甚至不是同一个问题。
  • 两个接受的答案都提到了相同的编辑 Web.Config 并且都提到添加 MIME 类型以防止 404 错误。另一个问题仅在标题中提及 IIS8,而不是标签。看起来两者都涉及两个问题:两者的 MIME 类型,一个是预发布软件,另一个是错字。我将删除我的近距离投票。
  • 这个问答也适用于v=4.3.0。它解决了我在项目中遇到的问题。谢谢

标签: asp.net asp.net-mvc-5 woff2


【解决方案1】:

文件格式woff2 位于mapping list 中,但这是最近添加的(2015 年 2 月),因此您不能使用包含此更改的版本。因此,要添加自定义文件格式,您可以使用 web.config 的 IIS 方式:

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

或者使用StaticFilesOptions:

public void Configure(IApplicationBuilder app)
{
    StaticFileOptions options = new StaticFileOptions();
    FileExtensionContentTypeProvider typeProvider = new FileExtensionContentTypeProvider();
    if (!typeProvider.Mappings.ContainsKey(".woff2"))
    {
        typeProvider.Mappings.Add(".woff2", "application/font-woff2");
    }
    options.ContentTypeProvider = typeProvider;
    app.UseStaticFiles(options);
}

【讨论】:

  • 你是对的,问题是因为我在此更改之前使用的是 alpha 版本。谢谢。
  • Web.config 代码中的 mime 类型需要为 application/font-woff2。 “2”不见了。
  • 谢谢,我已经修好了。
  • 映射列表的链接失效了,这是correct one。
  • 感谢这个有用的提示。从事我的项目。
【解决方案2】:

在您的 web.config 中添加 mime 类型声明

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

更多信息见:

Set mime types for web fonts in IIS

Quick fix: IIS .woff font file 404 not found in asp.net mvc

【讨论】:

  • 如果其他人有问题,请尝试删除 .在 .woff2 中,在我将上述 mimeMap 添加到我的 web.config 后,它为我修复了它。
【解决方案3】:

如果上述方法对您不起作用(对我不起作用)。然后试试这个:

<mimeMap fileExtension="woff2" mimeType="application/font-woff" />

【讨论】:

  • 也为我工作。我猜他们内置的条目包括 .在 fileExtension 中,因为当我添加带有 .woff2 作为扩展名的地图时,我收到一个错误,即存在重复条目。当我删除 .它开始工作了。很棒的发现!
【解决方案4】:

在 plesk 中添加 mime 类型

application/font-woff2  .woff2

对我有用

【讨论】:

  • 你能解释一下吗?
  • 在 plesk 主机面板中打开 Web 服务器设置,您可以找到 MIME 类型选项。只需在此处添加该 mime 类型即可。
【解决方案5】:

我需要先启用扩展(这也可以在 IIS 中完成),例如:

<system.webServer>
  ...
  <security>
    <requestFiltering>
      <fileExtensions>
        <remove fileExtension=".woff2" />
        <add fileExtension=".woff2" allowed="true" />
      </fileExtensions>
    </requestFiltering>
  </security>
...
</system.webServer>

以及添加前面提到的静态内容...

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-02-12
    • 2015-03-24
    • 2012-06-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-04-16
    相关资源
    最近更新 更多