【问题标题】:How does ASP.NET vNext handle Caching, Compression & MimeMap in config.json?ASP.NET vNext 如何处理 config.json 中的缓存、压缩和 MimeMap?
【发布时间】:2015-04-27 11:21:30
【问题描述】:

在以前的版本中,所有这些设置都可以使用如下代码在 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"/>

但是,随着 Web.Config 不再存在于 ASP.NET vNext 中,您如何调整这样的设置?我已经搜索了 netASP.NET Github 存储库,但没有发现任何东西 - 有什么想法吗?

【问题讨论】:

  • 如果您在 IIS 上托管您的网站,您仍然可以使用 web.config 文件

标签: asp.net asp.net-mvc caching compression asp.net-core


【解决方案1】:

正如 cmets 中的“agua from mars”所述,如果您使用 IIS,则可以使用 IIS 的静态文件处理,在这种情况下,您可以使用 web.config 文件中的 &lt;system.webServer&gt; 部分,这将作为总是这样。

如果您使用的是 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
        ...
    });
    ...
}

【讨论】:

    猜你喜欢
    • 2023-03-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-06-24
    • 2010-10-19
    • 2015-03-06
    • 2011-04-10
    • 2011-05-21
    相关资源
    最近更新 更多