ASP.NET vNext在config.json中如何处理缓存、压缩和MimeMap?

7
在之前的版本中,所有这些设置都可以通过像下面这样的代码在Web.Config文件中添加和调整:
<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"/>

然而,在ASP.NET vNext中,Web.Config已不再存在,那么如何调整此类设置呢?我在网上搜索了StackOverflowASP.NET Github仓库,但没有找到任何相关的信息 - 有任何想法吗?

2
如果您的网站托管在IIS上,仍然可以使用web.config文件。 - agua from mars
1个回答

8
如评论中所述,如果您使用的是IIS,则可以使用IIS的静态文件处理方式,在这种情况下,您可以在web.config文件中使用部分,并且它将像往常一样工作。
如果您正在使用ASP.NET 5的StaticFileMiddleware,则它具有自己的MIME映射,这是 FileExtensionContentTypeProvider实现的一部分。StaticFileMiddleware具有StaticFileOptions,您可以在初始化时在Startup.cs中使用它来配置它。在该选项类中,您可以设置内容类型提供程序。您可以实例化默认内容类型提供程序,然后只需调整映射字典,或者您可以从头开始编写整个映射(不建议)。

ASP.NET Core - mime映射:

如果您提供给整个站点的文件类型不会改变,您可以配置单个ContentTypeProvider类的实例,然后利用DI在提供静态文件时使用它,如下所示:

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
        ...
    });
    ...
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接