How to add Mime Types in ASP.NET Core
Asked Answered
B

2

16

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?

Bettis answered 9/8, 2018 at 14:48 Comment(5)
See: learn.microsoft.com/en-us/dotnet/api/…Vey
Behind IIS you can do it the same way, but don't forget to add UseStaticFiles in your Configure method of your Startup classRishi
@DanWilson I've read the documentation but it doesn't explain how to implement it.Bettis
my asp.net core mvc app doesnt have a web.config. where would I do this?Pasta
Mapping detail in ASP.Net CoreNigelniger
P
29

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
    });
Pyrrhuloxia answered 9/8, 2018 at 14:56 Comment(0)
C
1

For ASP.NET Core applications, IIS changes won't work.

In your Startup.cs file's Configure method, make the following changes:

app.UseStaticFiles(new StaticFileOptions()
{
    ContentTypeProvider = new FileExtensionContentTypeProvider(new Dictionary<string, string>
    {
      { 
         ".apk",
         "application/vnd.android.package-archive"
       }
    })
});
Chondrite answered 28/11, 2023 at 12:0 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.