Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.NET Core - Download .exe returns 404 error

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:

enter image description here

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" />

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.

How do I fix this?

like image 672
Smithy Avatar asked Mar 16 '17 10:03

Smithy


1 Answers

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
});
like image 121
Joel Harkes Avatar answered Nov 05 '22 21:11

Joel Harkes