【发布时间】: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