Tags: .NET 9

Add extra MIME type mappings in .NET 9

In the good old days of .NET Framework, additional MIME type mappings could be added to web.config like this:

<system.webServer>
    <staticContent>
        <mimeMap fileExtension="woff" mimeType="application/font-woff" />
        <mimeMap fileExtension=".woff2" mimeType="application/font-woff2" />
    </staticContent>
</system.webServer>

In .NET Core (.NET 5 / .NET 6 / .NET 7 / .NET 8 / .NET 9), you can add MIME type mappings programmatically like this:

public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
    var provider = new FileExtensionContentTypeProvider();
    provider.Mappings[".woff"] = "application/font-woff";
    provider.Mappings[".woff2"] = "application/font-woff2";

    app.UseStaticFiles(new StaticFileOptions()
    {
        ContentTypeProvider = provider
    });
}

That's it!

Found this post helpful? Help keep this blog ad-free by buying me a coffee! ☕