使用Asp.Net VNext时,application/font-woff2无法正常工作。

60

我正在使用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。它们不是同样的解决方案,甚至不是同样的问题。 - Austin Felipe
两个被接受的答案都提到了编辑Web.Config文件,并且都提到了添加MIME类型以防止404错误。另一个问题仅在标题中提到了IIS8,而没有标签。看起来这两个问题都涉及到了两个问题:两者都需要MIME类型,其中一个还涉及到预发布软件,另一个则是一个错别字。我将撤销我的关闭投票。 - Charles Burns
这个问题和答案同样适用于v=4.3.0。它解决了我在项目中遇到的问题。谢谢。 - vivek
5个回答

131

woff2 文件格式在映射列表中,但该格式是近期添加的(2015年2月),因此您可能无法使用包含此更改的版本。如果要添加自定义文件格式,可以使用 IIS 的方法,即使用 web.config 文件:

<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版本。谢谢。 - Austin Felipe
您的Web.config代码中的MIME类型应为application/font-woff2。缺少“2”。 - Bassem
谢谢,我已经修复了。 - meziantou
感谢这个有用的提示。在我的项目上起作用了。 - Sedat Kumcu
当前的标准 MIME 类型是 font/woff2,请参见 https://www.w3.org/TR/WOFF2/#IMT 或链接的映射列表。 - Sacha K
显示剩余2条评论

16

1
如果其他人遇到问题,请尝试将".woff2"中的"."删除,我在添加了上述mimeMap到我的web.config之后就解决了这个问题。 - devfunkd

2
如果上面的方法对您没有用(对我来说也没用),那么请尝试使用以下方法:
<mimeMap fileExtension="woff2" mimeType="application/font-woff" />

对我也起作用了。我猜他们建立的条目包括文件扩展名中的“.”,因为当我添加以“.woff2”为扩展名的映射时,会出现重复条目的错误。当我删除“.”后,它开始工作了。太好了! - Mike Devenney

0

我需要先启用扩展名(在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>

0
在 Plesk 中添加 MIME 类型
application/font-woff2  .woff2

这对我有用


你能稍微解释一下吗? - Dieter Meemken
在 Plesk 托管面板中打开 Web 服务器设置,您可以找到 MIME 类型选项。只需在那里添加该 MIME 类型即可。 - Prince Prasad

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接