When developing an application using .NET Framework 4.6 (MVC4/5), I used to add custom mime types in the web.config file, like this (this is the actual mime types I need to add in my app):
<system.webServer>
<staticContent>
<mimeMap fileExtension=".wasm" mimeType="application/wasm"/>
<mimeMap fileExtension="xap" mimeType="application/x-silverlight-app"/>
<mimeMap fileExtension="xaml" mimeType="application/xaml+xml"/>
<mimeMap fileExtension="xbap" mimeType="application/x-ms-xbap"/>
</staticContent>
How can I replicate this behaviour in a .NET Core? Is it possible to do it the same way?
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
This configuration is for the web server, not for ASP.NET. The system.webServer section in the web.config deals with the configuration of IIS.
If your ASP.NET Core application is running behind IIS and IIS is handling the static content, then you should continue to use the same thing.
If you are using nginx, you can add mime types to your configuration or edit the mime.types file. If you are using a different web server, consult the documentation for the web server.
If ASP.NET Core is handling the static content itself and is running at the edge, or if you need ASP.NET Core to be aware of mime types, you need to configure ASP.NET Core’s handler to be aware of it. This is explained in the documentation.
An example from the documentation:
public void Configure(IApplicationBuilder app)
{
var provider = new FileExtensionContentTypeProvider();
// Add new mappings
provider.Mappings[".myapp"] = "application/x-msdownload";
app.UseStaticFiles(new StaticFileOptions
{
FileProvider = new PhysicalFileProvider(
Path.Combine(Directory.GetCurrentDirectory(), "wwwroot", "images")),
RequestPath = "/StaticContentDir",
ContentTypeProvider = provider
});
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0