For ASP.NET Core applications, IIS changes won't work.
In your Startup.cs
file's Configure
method, make the following changes:
app.UseStaticFiles(new StaticFileOptions()
{
ContentTypeProvider = new FileExtensionContentTypeProvider(new Dictionary<string, string>
{
{ ".apk","application/vnd.android.package-archive"}
})
});
A better option is to read extension/ mime type pairs from config and inject it into the Config method like this so that extensions can be managed from configuration:
appsettings.json
:
"StaticFilesConfig": {
"AllowedExtensions": {
".apk": "application/vnd.android.package-archive",
".ext": "application/ext-example-mimetype" // example
}
}
Startup.cs
:
public void ConfigureServices(IServiceCollection services)
{
....
services.Configure<StaticFilesConfig>(Configuration.GetSection("StaticFilesConfig"));
....
}
public void Configure(IApplicationBuilder app, IWebHostEnvironment env
, ILoggerFactory loggerFactory, IOptions<StaticFilesConfig> staticFilesConfig)
{
...
if (staticFilesConfig != null)
{
SetStaticFileOptions(app, staticFilesConfig);
}
...
}
private void SetStaticFileOptions(IApplicationBuilder app
, IOptions<StaticFilesConfig> staticFilesOptions)
{
var mapping = staticFilesOptions.Value.AllowedExtensions;
if (mapping != null && mapping.Any())
{
var provider =
new FileExtensionContentTypeProvider(staticFilesOptions.Value.AllowedExtensions);
app.UseStaticFiles(new StaticFileOptions
{
ContentTypeProvider = provider
});
}
}
}
StaticFilesConfig.cs
:
public class StaticFilesConfig
{
public IDictionary<string, string> AllowedExtensions { get; set; }
}