ASP.NET Core-下载.exe返回404错误 [英] ASP.NET Core - Download .exe returns 404 error

查看:235
本文介绍了ASP.NET Core-下载.exe返回404错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a ASP.NET core MVC application and in the wwwroot folder, I've added another folder called "Shaun" and in that folder I've dropped an exe to try and download:

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

Now if I navigate to: http://localhost:PORT/Shaun/chromesetup.exe I get a 404 error. I've tried adding the Handler below but this doesn't work.

 <add name="Client exe" path="*.exe" verb="*" modules="StaticFileModule" resourceType="File" />

其他信息:之所以需要这样做,是因为我有一个连接到该网站的客户端应用程序,该客户端应用程序是使用ClickOnce打包并放入网站的wwwroot中的,以前使用MVC可以(以前核心),现在仍然可以,但核心不支持.

Extra info: The reason I need to do this is because I have a client application that connects to this website, that client application is packaged using ClickOnce and gets dropped into the wwwroot of the website, this previously worked using MVC (pre Core) and still does, but doesn't with core.

我该如何解决?

推荐答案

尝试以下操作并告诉我是否可行:

Try the following and tell me if it works:

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.
}

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

You can look at the sourcecode of StaticFileMiddleware to see how it handles static files.

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

On default the FileExtensionContentTypeProvider is used to check based on filename extensions what ContentType needs to be return in the Http Response headers. exe is not in this list.

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

So another option would be to add Exe to this list:

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

这篇关于ASP.NET Core-下载.exe返回404错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆