Skip to content
This repository has been archived by the owner on Dec 14, 2018. It is now read-only.

APK packages is not served in ASP.NET Core MVC #7739

Closed
Nefcanto opened this issue May 7, 2018 · 3 comments
Closed

APK packages is not served in ASP.NET Core MVC #7739

Nefcanto opened this issue May 7, 2018 · 3 comments

Comments

@Nefcanto
Copy link

Nefcanto commented May 7, 2018

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?

@khellang
Copy link
Contributor

khellang commented May 7, 2018

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.

@Nefcanto
Copy link
Author

Nefcanto commented May 7, 2018

Nice explanation. Thank you.

@mkArtakMSFT
Copy link
Member

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.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants