Florian F.
Florian F.

Reputation: 4700

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 ?

Upvotes: 1

Views: 1301

Answers (2)

Pawel Gorczynski
Pawel Gorczynski

Reputation: 1305

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

Upvotes: 3

Florian F.
Florian F.

Reputation: 4700

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.

Upvotes: 9

Related Questions