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.