ASP.NET Core - Download .exe returns 404 error

前端 未结 2 1575
一生所求
一生所求 2021-01-17 13:09

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:

2条回答
  •  没有蜡笔的小新
    2021-01-17 13:30

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

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

    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.

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

提交回复
热议问题