ASP.NET Core - 下载.exe返回404错误

Smi*_*thy 9 c# asp.net asp.net-mvc download asp.net-core

我有一个ASP.NET核心MVC应用程序,在wwwroot文件夹中,我添加了另一个名为"Shaun"的文件夹,在该文件夹中我删除了一个exe来尝试下载:

在此输入图像描述

现在,如果我导航到:http:// localhost:PORT/Shaun/chromesetup.exe我收到404错误.我尝试在下面添加处理程序,但这不起作用.

 <add name="Client exe" path="*.exe" verb="*" modules="StaticFileModule" resourceType="File" />
Run Code Online (Sandbox Code Playgroud)

额外信息:我需要这样做的原因是因为我有一个客户端应用程序连接到这个网站,该客户端应用程序使用ClickOnce打包并被放入网站的wwwroot,这以前使用MVC(pre Core)和仍然如此,但没有核心.

我该如何解决?

Joe*_*kes 16

尝试以下操作并告诉我它是否有效:

app.UseStaticFiles(new StaticFileOptions
{
    ServeUnknownFileTypes = true, //allow unkown file types also to be served
    DefaultContentType = "Whatver you want eg: plain/text" //content type to returned if fileType is not known.
}
Run Code Online (Sandbox Code Playgroud)

您可以查看源代码,StaticFileMiddleware了解它如何处理静态文件.

默认情况下,FileExtensionContentTypeProvider用于根据文件扩展名检查ContentType需要在Http Response标头中返回的内容.exe不在此列表中.

所以另一种选择是将Exe添加到此列表中:

var provider = new FileExtensionContentTypeProvider();
provider.Mappings.Add(".exe", "application/vnd.microsoft.portable-executable"); //file ext, ContentType
app.UseStaticFiles(new StaticFileOptions
{
    ContentTypeProvider = provider
});
Run Code Online (Sandbox Code Playgroud)