APK包不在ASP.NET Core MVC中提供 [英] APK packages is not served in ASP.NET Core MVC

查看:110
本文介绍了APK包不在ASP.NET Core MVC中提供的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在我的ASP.NET Core MVC项目中提供.apk文件,但由于它返回404,所以我不能.

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

我使用VS 2017中的默认模板创建了一个简单的ASP.NET Core MVC项目,并将.apk文件添加到wwwroot文件夹中,然后我尝试使用/application.apk路径进行访问,但并没有不能工作,而/favicon.ico可以工作,而该目录中的其他静态内容也可以工作,因为默认情况下会调用模板app.UseStaticFiles().

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.

然后我尝试更改配置,将静态内容从wwwroot文件夹中取出,并将application.apk放置在项目的根目录(从wwwroot向上的一个文件夹)中,并使用以下命令配置项目:

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

没有结果.我得到404.

Again no result. I get 404.

然后我创建了一个Web.config文件并添加了此MIME类型:

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

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

仍然,不提供.apk文件.

我该怎么办?

推荐答案

ASP.NET Core静态文件中间件显然不提供ContentTypeProvider中没有映射到MIME类型的文件.要解决此问题,您应该将MIME映射添加到ContentTypeProvider:

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

现在将提供.apk.

这篇关于APK包不在ASP.NET Core MVC中提供的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆