How To Enable .exe Downloads From ASP.net Core Websites

How To Enable .exe Downloads From ASP.net Core Websites

Enabling .exe Downloads on ASP.net Core Websites

Out of the box, ASP.net Core does not host .exe files. This is because the default MIME mappings for the static file host does not include .exe. To resolve this, we need to tell the UseStaticFiles() middleware to recognize these files.

When we ran into this issue, we were hosting an .exe installer for direct download and for WinSparkle updates. A quick web search will point at Web.config methods of solving this issue, but they do not work, as IIS doesn't ever see these requests. The answer is in the ASP.net Core documentation; in the Startup.cs' Configure() method, one has to add a mapping for .exe (or any other file type that doesn't have a mapping in the default set).

If you don't have it already, tell ASP.net to host static files:

public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
    app.UseStaticFiles();
}

Now, create an instance of the default FileExtensionContentTypeProvider, but with the mappings you want to add. I added a method for this to keep the Configure() method a bit cleaner.

private StaticFileOptions GetStaticFileConfiguration()
{
    var provider = new FileExtensionContentTypeProvider();
    provider.Mappings[".exe"] = "application/octect-stream";
    return new StaticFileOptions { ContentTypeProvider = provider };
}

And, pass it into the UseStaticFiles() call.

public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
    app.UseStaticFiles(GetStaticFileConfiguration());
}

After adding the mapping and restarting the web server, the server will now host .exe files.

Also, here are the default mappings in ASP.net Core 1.1.2: link These were generated by looping over the provider's Mappings Dictionary.

SHARE


comments powered by Disqus

Follow Us

Latest Posts

subscribe to our newsletter