如何ASP.NET vNext处理缓存,COM pression&安培; MimeMap在config.json? [英] How does ASP.NET vNext handle Caching, Compression & MimeMap in config.json?

查看:233
本文介绍了如何ASP.NET vNext处理缓存,COM pression&安培; MimeMap在config.json?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在previous版本的所有的这些设置可以添加和使用类似下面的code在Web.config文件中调整:

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"/>

不过,与Web.Config中不再显示于ASP.NET vNext身边,你怎么调整这样的设置?我已经搜查了<一个href=\"http://stackoverflow.com/questions/27836176/asp-net-vnext-enable-com$p$pssion-to-iis-8-on-azure\">net和 ASP.NET Github上回购,但没有碰到任何东西 - 任何想法

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?

推荐答案

由于各国的意见,如果你使用IIS,你可以使用IIS的静态文件处理来自火星阿瓜,在这种情况下,你可以使用&LT; system.webServer方式&gt; 在web.config文件部分,将工作,因为它总是这样

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.

如果你使用ASP.NET 5的StaticFileMiddleware那么它有它自己的MIME映射而来为的<一部分href=\"https://github.com/aspnet/StaticFiles/blob/d16a73cc05d842390768f78134c1eeb794215fae/src/Microsoft.AspNet.StaticFiles/FileExtensionContentTypeProvider.cs\"相对=nofollow> FileExtensionContentTypeProvider 实施。在 StaticFileMiddleware 有一个 StaticFileOptions ,你可以用它来配置它,当你在初始化启动的.cs 。在选项类别可设置内容类型提供商。您可以实例默认内容类型提供程序,然后只是调整的映射字典,也可以从头开始编写一个完整的映射(不推荐)。

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

如果扩展集,你正在为整个网站的文件类型都不会改变,你可以配置 ContentTypeProvider 类的一个实例,然后再利用DI服务于静态文件的时候,像这样使用它:

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

这篇关于如何ASP.NET vNext处理缓存,COM pression&安培; MimeMap在config.json?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆