APK packages is not served in ASP.NET Core MVC

回眸只為那壹抹淺笑 提交于 2019-12-22 07:01:51

问题


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?


回答1:


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.



来源:https://stackoverflow.com/questions/49402090/apk-packages-is-not-served-in-asp-net-core-mvc

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!