【问题标题】:How to serve woff2 files from owin FileServer如何从 owin FileServer 提供 woff2 文件
【发布时间】:2015-04-11 23:39:27
【问题描述】:

从 font awesome 4.3 开始,他们将字体添加为 woff2 格式。

我在尝试通过 owin 提供此文件时遇到 404ed 错误:

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

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

【问题讨论】:

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


    【解决方案1】:

    两种可能:

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

    【讨论】:

      【解决方案2】:

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

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

      在哪里

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

      【讨论】:

      • 这是一个非常好的方法,因为它使将来添加类型变得容易。这两个答案对我都很有用,但我最终用这个来解决我的问题。
      猜你喜欢
      • 2021-11-15
      • 2023-03-08
      • 2020-06-30
      • 2016-11-21
      • 2014-10-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-04-10
      相关资源
      最近更新 更多