Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

APK packages is not served in ASP.NET Core 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 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?

like image 867
mohammad rostami siahgeli Avatar asked Mar 21 '18 09:03

mohammad rostami siahgeli


1 Answers

The ASP.NET Core Static files middleware apparently doesn't serve files which don't have a mapping to a MIME type in the ContentTypeProvider. To solve this, you should add the MIME mapping to the ContentTypeProvider:

    FileExtensionContentTypeProvider contentTypes = new FileExtensionContentTypeProvider();
    contentTypes.Mappings[".apk"] = "application/vnd.android.package-archive";
    app.UseStaticFiles(new StaticFileOptions
    {
        ContentTypeProvider = contentTypes
    });

Now the .apk will be served.

like image 129
Mateo Velenik Avatar answered Sep 28 '22 14:09

Mateo Velenik