Mvc: APK packages is not served in ASP.NET Core MVC

Created on 7 May 2018  路  3Comments  路  Source: aspnet/Mvc

I want to serve .apk files in my ASP.NET Core MVC project, and I can't since it returns 404.

I created a simple ASP.NET Core MVC project using default template in VS 2017, and added the .apk file to wwwroot folder, and then I tried to reach it using /application.apk path, and it didn't work, while /favicon.ico works, and other static contents in that directory work, because in the default template app.UseStaticFiles() is called.

I then tried to change configurations, brought static contents out of wwwroot folder and put the application.apk in the root directory of the project (one folder up from wwwroot) and configured the project using:

app.UseStaticFiles(new StaticFileOptions()
{
    FileProvider = new PhysicalFileProvider(Directory.GetCurrentDirectory())
});

Again no result. I get 404.

Then I created a Web.config file and added this MIME Type:

<staticContent>
  <mimeMap fileExtension=".apk" mimeType="application/vnd.android.package-archive" />
</staticContent>

Still, doesn't serve the .apk file.

What should I do?

Most helpful comment

Then I created a Web.config file and added this MIME Type

web.config is for configuring IIS. Unless you're using IIS to serve static files, this will make no difference.

As stated in the documentation:

The static file middleware understands almost 400 known file content types. If the user requests a file of an unknown file type, the static file middleware returns a HTTP 404 (Not Found) response.

If you want to serve APK-files using the static files middleware, you can either turn on ServeUnknownFileTypes, or add the extension using FileExtensionContentTypeProvider.

Adding extra extensions using FileExtensionContentTypeProvider (recommended)

var provider = new FileExtensionContentTypeProvider();

provider.Mappings[".apk"] = "application/vnd.android.package-archive";

app.UseStaticFiles(new StaticFileOptions
{
    FileProvider = new PhysicalFileProvider(Directory.GetCurrentDirectory()),
    ContentTypeProvider = provider
});

This is also outlined in the documentation.

Serving unknown file types

app.UseStaticFiles(new StaticFileOptions
{
    FileProvider = new PhysicalFileProvider(Directory.GetCurrentDirectory()),
    ServeUnknownFileTypes = true
});

But be aware that this is discouraged as it could be a security risk:

Enabling ServeUnknownFileTypes is a security risk. It's disabled by default, and its use is discouraged. FileExtensionContentTypeProvider provides a safer alternative to serving files with non-standard extensions.

This is also outlined in the documentation.

All 3 comments

Then I created a Web.config file and added this MIME Type

web.config is for configuring IIS. Unless you're using IIS to serve static files, this will make no difference.

As stated in the documentation:

The static file middleware understands almost 400 known file content types. If the user requests a file of an unknown file type, the static file middleware returns a HTTP 404 (Not Found) response.

If you want to serve APK-files using the static files middleware, you can either turn on ServeUnknownFileTypes, or add the extension using FileExtensionContentTypeProvider.

Adding extra extensions using FileExtensionContentTypeProvider (recommended)

var provider = new FileExtensionContentTypeProvider();

provider.Mappings[".apk"] = "application/vnd.android.package-archive";

app.UseStaticFiles(new StaticFileOptions
{
    FileProvider = new PhysicalFileProvider(Directory.GetCurrentDirectory()),
    ContentTypeProvider = provider
});

This is also outlined in the documentation.

Serving unknown file types

app.UseStaticFiles(new StaticFileOptions
{
    FileProvider = new PhysicalFileProvider(Directory.GetCurrentDirectory()),
    ServeUnknownFileTypes = true
});

But be aware that this is discouraged as it could be a security risk:

Enabling ServeUnknownFileTypes is a security risk. It's disabled by default, and its use is discouraged. FileExtensionContentTypeProvider provides a safer alternative to serving files with non-standard extensions.

This is also outlined in the documentation.

Nice explanation. Thank you.

Thanks for contacting us. We believe that the question you've raised have been answered. If you still feel a need to continue the discussion, feel free to reopen it and add your comments.

Was this page helpful?
0 / 5 - 0 ratings