下载 link 的 exe 文件导致 "Not Found" 错误

Download link for exe files results in "Not Found" error

我有一个 ASP.NET Core Razor Pages 应用程序,我想添加一个 link,它会在单击时下载一个 exe 文件。因此,我将以下代码添加到我的 Startup class.

Startup.cs

app.UseStaticFiles();
app.UseStaticFiles(new StaticFileOptions
{
    FileProvider = new PhysicalFileProvider(Path.Combine(Directory.GetCurrentDirectory(), "folder1")),
    RequestPath = "/folder1"
});
var provider = new FileExtensionContentTypeProvider();
provider.Mappings.Add(".exe", "application/octect-stream");
app.UseStaticFiles(new StaticFileOptions
{
    ContentTypeProvider = provider
});

Razor 视图

<a id="installButton" href="https://mysite.net.au/folder1/install/setup.exe">Install</a>

当我点击 link 时,出现以下错误。

Status Code: 404; Not Found

我也试过使用 application/vnd.microsoft.portable-executable 作为 MIME 类型,但得到了同样的错误。

更新#1: 我知道这个问题已经 posted - 我在 posting 之前做了很多搜索和阅读,但是 none 的建议解决方案解决了这个问题。因此我需要 post 我的确切 code/situation.

更新#2 没有按钮可以回答我的问题,所以我就在这里提供: 我能让它工作的唯一方法是添加两个静态文件选项。 这是有效的代码:

var provider = new FileExtensionContentTypeProvider();
provider.Mappings.Add(".exe", "application/octect-stream");
app.UseStaticFiles(new StaticFileOptions
{
   FileProvider = new physicalFileProvider(Path.Combine(Directory.GetCurrentDirectory(), "folder1")),
   RequestPath = "/folder1",
   ServeUnknownFileTypes = true,
   DefaultContentType = "plain/text",
   ContentTypeProvider = provider
});

我测试了你的代码并发现了问题。您必须将最后两个 UseStaticFiles 声明合并为一个。

app.UseStaticFiles();
var provider = new FileExtensionContentTypeProvider();
provider.Mappings.Add(".exe", "application/octect-stream");
app.UseStaticFiles(new StaticFileOptions
{
    FileProvider = new PhysicalFileProvider(Path.Combine(Directory.GetCurrentDirectory(), "static")),
    RequestPath = "/folder1",
    ContentTypeProvider = provider
});