我有一个ASP.NET核心剃刀页面应用程序,并想添加一个链接,当点击下载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
});
发布于 2019-03-24 23:50:11
我测试了你的代码,发现了问题所在。您必须将最后两个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
});
https://stackoverflow.com/questions/55325296
复制相似问题