如何从owin FileServer提供woff2文件

时间:2015-02-11 14:43:11

标签: mime-types owin font-awesome fileserver woff2

由于字体很棒4.3,他们将字体添加为woff2格式。

当我试图通过owin提供此文件时,我正在问404:

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

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

2 个答案:

答案 0 :(得分:8)

两种可能性:

  • 提供各种文件类型:
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);

第二种选择似乎并不优雅,但仍然是最好的选择。阅读why mime types are important

答案 1 :(得分:1)

使用继承可以避免不太好的转换:

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

,其中

private class CustomFileExtensionContentTypeProvider : FileExtensionContentTypeProvider
{
    public CustomFileExtensionContentTypeProvider()
    {
        Mappings.Add(".json", "application/json");
        Mappings.Add(".mustache", "text/template");
    }
}