Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

In previous versions all of these settings could be added and tweaked in the Web.Config file using something like the code below:

<staticContent>
  <mimeMap fileExtension=".webp" mimeType="image/webp" />
  <!-- Caching -->
  <clientCache cacheControlMode="UseMaxAge" cacheControlMaxAge="96:00:00" />
</staticContent>
<httpCompression directory="%SystemDrive%\inetpub\temp\IIS Temporary Compressed Files">
  <scheme name="gzip" dll="%Windir%\system32\inetsrv\gzip.dll" />
  <dynamicTypes>
    <add mimeType="text/*" enabled="true" />
    <add mimeType="message/*" enabled="true" />
    <add mimeType="application/javascript" enabled="true" />
    <add mimeType="*/*" enabled="false" />
  </dynamicTypes>
  <staticTypes>
    <add mimeType="text/*" enabled="true" />
    <add mimeType="message/*" enabled="true" />
    <add mimeType="application/javascript" enabled="true" />
    <add mimeType="*/*" enabled="false" />
  </staticTypes>
</httpCompression>
<urlCompression doStaticCompression="true" doDynamicCompression="true"/>

However, with the Web.Config no longer being around in ASP.NET vNext, how do you adjust settings like this? I have searched the net and the ASP.NET Github repo, but not come across anything - any ideas?

like image 626
Deano Avatar asked Feb 25 '15 12:02

Deano


1 Answers

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
        ...
    });
    ...
}
like image 137
Eilon Avatar answered Nov 15 '22 14:11

Eilon