18

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?

5
  • See: learn.microsoft.com/en-us/dotnet/api/… Commented Aug 9, 2018 at 14:54
  • Behind IIS you can do it the same way, but don't forget to add UseStaticFiles in your Configure method of your Startup class Commented Aug 9, 2018 at 14:58
  • @DanWilson I've read the documentation but it doesn't explain how to implement it. Commented Aug 9, 2018 at 20:20
  • my asp.net core mvc app doesnt have a web.config. where would I do this? Commented Jul 11, 2019 at 21:54
  • Mapping detail in ASP.Net Core Commented Feb 1, 2020 at 11:57

2 Answers 2

32

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
    });
Sign up to request clarification or add additional context in comments.

Comments

2

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"
       }
    })
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.