加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > asp.Net > 正文

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

发布时间:2020-12-15 23:59:31 所属栏目:asp.Net 来源:网络整理
导读:在以前的版本中,可以使用类似下面的代码在Web.Config文件中添加和调整所有这些设置: staticContent mimeMap fileExtension=".webp" mimeType="image/webp" / !-- Caching -- clientCache cacheControlMode="UseMaxAge" cacheControlMaxAge="96:00:00" //sta
在以前的版本中,可以使用类似下面的代码在Web.Config文件中添加和调整所有这些设置:
<staticContent>
  <mimeMap fileExtension=".webp" mimeType="image/webp" />
  <!-- Caching -->
  <clientCache cacheControlMode="UseMaxAge" cacheControlMaxAge="96:00:00" />
</staticContent>
<httpCompression directory="%SystemDrive%inetpubtempIIS Temporary Compressed Files">
  <scheme name="gzip" dll="%Windir%system32inetsrvgzip.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中,你如何调整这样的设置?我搜索了net和ASP.NET Github回购,但没有遇到任何问题 – 任何想法?

解决方法

正如“火星中的agua”在评论中指出的那样,如果您使用的是IIS,则可以使用IIS的静态文件处理,在这种情况下,您可以使用< system.webServer> web.config文件中的部分,它将一如既往地工作.

如果您使用的是ASP.NET 5的StaticFileMiddleware,那么它有自己的MIME映射,它们是FileExtensionContentTypeProvider实现的一部分. StaticFileMiddleware有一个StaticFileOptions,您可以在Startup.cs中初始化它时使用它来配置它.在该选项类中,您可以设置内容类型提供程序.您可以实例化默认内容类型提供程序,然后只需调整映射字典,或者您可以从头开始编写整个映射(不推荐).

ASP.NET核心 – 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
        ...
    });
    ...
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读