How to serve woff2 files from owin FileServer

Since font awesome 4.3, they added the fonts as woff2 format.

I'm guetting 404ed when trying to serve this file through owin :

app.UseFileServer(new FileServerOptions() {
    RequestPath = PathString.Empty,
    FileSystem = new PhysicalFileSystem(@"banana")
});

How do I serve woff2 mime type files through file server in owin ?


Two possibilities :

  • Serve all kind of file types :
  • var options = new FileServerOptions() {
        RequestPath = PathString.Empty,
        FileSystem = new PhysicalFileSystem(@"banana")
    };
    
    options.StaticFileOptions.ServeUnknownFileTypes = true;
    
    app.UseFileServer(options);
    
  • Add woff2 mime type :
  • var options = new FileServerOptions() {
        RequestPath = PathString.Empty,
        FileSystem = new PhysicalFileSystem(@"banana")
    };
    
    ((FileExtensionContentTypeProvider)options.StaticFileOptions.ContentTypeProvider)
        .Mappings.Add(".woff2", "application/font-woff2");
    
    app.UseFileServer(options);
    

    Second options seems not as elegant but is nonetheless the best. Read why mime types are important.


    You can avoid the not-very-nice casting by using inheritance:

    FileServerOptions options = new FileServerOptions
    {
        StaticFileOptions =
        {
            ContentTypeProvider = new CustomFileExtensionContentTypeProvider(),
        }
    };
    

    where

    private class CustomFileExtensionContentTypeProvider : FileExtensionContentTypeProvider
    {
        public CustomFileExtensionContentTypeProvider()
        {
            Mappings.Add(".json", "application/json");
            Mappings.Add(".mustache", "text/template");
        }
    }
    
    链接地址: http://www.djcxy.com/p/47008.html

    上一篇: .woff2无法在Chrome中加载

    下一篇: 如何从owin FileServer提供woff2文件