Add a mime-type to an ASP.NET Core (.NET 6) app

If you have a .NET 6 app with static files and you put in a file with a non-web standard extension somewhere in your WWWROOT you will possibly get a 404 if you try to access the file. This is because the app.UseStaticFiles(); option you add to your Program.CS file has a predefined list of files it will deliver from your site. For security reasons, anything not on this list gets blocked.

But the good news is, you can tell the app to add your file type to the Green Lit list. You just need to add a CotentTypeProvider to the StaticFileOptions passed to your UseStaticFiles method. In the code sample below, I am adding a proprietary .JSON file format with the extension of .RDLX-JSON which is blocked by default.

You still need to make a second, empty call for app.UseStaticFiles(); to have it serve the default filetypes.

var provider = new Microsoft.AspNetCore.StaticFiles.FileExtensionContentTypeProvider();

provider.Mappings.Add(new KeyValuePair<string, string>(".rdlx-json", "application/json"));

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

app.UseStaticFiles();

Now, when you rebuild your app, your file should be accessed via a URL like other web files.

How to check which version of .Net Core you have installed.

Open a command prompt and enter the version command.

dotnet --version
dotnet version

This should return a version number. If you get something like the message below, .Net Core is not installed.

'dotnet' is not recognized as an internal or external command,
operable program or batch file.

You can also issue the info command to get a lot more information about your environment.

dotnet --info
dotnet info

If you want to actually look at the different .Net SDKs which you have installed, they can be found here on a Windows machine:

C:\Program Files\dotnet\sdk