How does ASP.NET vNext handle Caching, Compression & MimeMap in config.json?

若如初见. 提交于 2019-11-29 13:48:10
Eilon

As "agua from mars" states in the comments, if you're using IIS you can use IIS's static file handling, in which case you can use the <system.webServer> section in a web.config file and that will work as it always did.

If you're using ASP.NET 5's StaticFileMiddleware then it has its own MIME mappings that come as part of the FileExtensionContentTypeProvider implementation. The StaticFileMiddleware has a StaticFileOptions that you can use to configure it when you initialize it in Startup.cs. In that options class you can set the content type provider. You can instantiate the default content type provider and then just tweak the mappings dictionary, or you can write an entire mapping from scratch (not recommended).


ASP.NET Core - mime mappings:

If the extended set of file types you are providing for the entire site are not going to change, you can configure a single instance of the ContentTypeProvider class, and then leverage DI to use it when serving static files, like so:

public void ConfigureServices(IServiceCollection services) 
{
    ...
    services.AddInstance<IContentTypeProvider>(
        new FileExtensionConentTypeProvider(
            new Dictionary<string, string>(
                // Start with the base mappings
                new FileExtensionContentTypeProvider().Mappings,
                // Extend the base dictionary with your custom mappings
                StringComparer.OrdinalIgnoreCase) {
                    { ".nmf", "application/octet-stream" }
                    { ".pexe", "application/x-pnal" },
                    { ".mem", "application/octet-stream" },
                    { ".res", "application/octet-stream" }
                }
            )
        );
    ...
}

public void Configure(
    IApplicationBuilder app, 
    IContentTypeProvider contentTypeProvider)
{
    ...
    app.UseStaticFiles(new StaticFileOptions() {
        ContentTypeProvider = contentTypeProvider
        ...
    });
    ...
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!