如何从owin FileServer提供woff2文件

由于字体真棒4.3,他们添加了字体作为woff2格式。

我试图通过owin来服务这个文件,

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

如何通过owin中的文件服务器为woff2 mime类型文件提供服务?


两种可能性:

  • 提供各种文件类型:
  • var options = new FileServerOptions() {
        RequestPath = PathString.Empty,
        FileSystem = new PhysicalFileSystem(@"banana")
    };
    
    options.StaticFileOptions.ServeUnknownFileTypes = true;
    
    app.UseFileServer(options);
    
  • 添加woff2 mime类型:
  • var options = new FileServerOptions() {
        RequestPath = PathString.Empty,
        FileSystem = new PhysicalFileSystem(@"banana")
    };
    
    ((FileExtensionContentTypeProvider)options.StaticFileOptions.ContentTypeProvider)
        .Mappings.Add(".woff2", "application/font-woff2");
    
    app.UseFileServer(options);
    

    第二选择似乎不是优雅,但仍然是最好的。 阅读为什么MIME类型很重要。


    您可以通过使用继承来避免不太好的转换:

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

    哪里

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

    上一篇: How to serve woff2 files from owin FileServer

    下一篇: Why isn't woff2 in the mimetype directory?