application/font-woff2 not working when using Asp.Net VNext

Austin Felipe picture Austin Felipe · Feb 26, 2015 · Viewed 41.7k times · Source

I'm doing some experiments with VNext + OSX + Chrome. I'm trying to get a woff2 file

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

But an error occur. See the request's header below

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

This is my 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" });
        });
    }

I saw inside AspNet project at Github about StaticFiles (Link bellow) and it seems to be supported.

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

Can you guys give me some help?

Answer

meziantou picture meziantou · Mar 27, 2015

The file format woff2 is in the mapping list but this was added recently (February 2015) so you may not use a release that contains this change. So to add a custom file format you can use the IIS way using web.config:

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

Or using 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);
}