Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add Mime Types in ASP.NET Core

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?

like image 441
Rambo3 Avatar asked Aug 09 '18 14:08

Rambo3


1 Answers

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
    });
like image 59
vcsjones Avatar answered Nov 09 '22 20:11

vcsjones