【问题标题】:Download link for exe files results in "Not Found" error [duplicate]exe文件的下载链接导致“未找到”错误[重复]
【发布时间】:2019-03-24 15:20:45
【问题描述】:

我有一个 ASP.NET Core Razor Pages 应用程序,想要添加一个链接,单击该链接可下载 exe 文件。因此,我将以下代码添加到我的Startup 类中。

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

剃刀视图

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

当我点击链接时,我收到以下错误。

状态码:404;没找到

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

更新 #1: 我知道这个问题已经发布了——我在发布之前做了很多搜索和阅读,但没有一个建议的解决方案能解决这个问题。因此我需要发布我的确切代码/情况。

更新 #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
});

【问题讨论】:

  • 我阅读了这篇文章并尝试了这两种方法,但我一直找不到 404。太令人沮丧了 - 不明白为什么它不起作用。
  • 你是对的。这不是重复的。

标签: c# asp.net-core razor-pages


【解决方案1】:

我测试了您的代码并发现了问题。您必须将最后两个 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
});

【讨论】:

  • 感谢 volkmar。我试过这个,但它仍然失败。我被否决了,因为这个问题已经有了答案,但它对我不起作用,所以我发布了我的确切代码。
猜你喜欢
  • 2013-05-11
  • 1970-01-01
  • 1970-01-01
  • 2018-10-09
  • 1970-01-01
  • 2015-01-13
  • 1970-01-01
  • 2012-12-26
  • 1970-01-01
相关资源
最近更新 更多