Dea*_*ano 7 compression asp.net asp.net-mvc caching asp.net-core
在以前的版本中,可以使用类似下面的代码在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"/>
Run Code Online (Sandbox Code Playgroud)
但是,由于Web.Config不再出现在ASP.NET vNext中,你如何调整这样的设置?我搜索过网络和ASP.NET Github仓库,但没有遇到任何问题 - 任何想法?
正如"火星中的agua"在评论中指出的那样,如果您使用的是IIS,则可以使用IIS的静态文件处理,在这种情况下,您可以使用<system.webServer>web.config文件中的该部分,并且它将一如既往地工作.
如果您使用的是ASP.NET 5的StaticFileMiddleware,那么它有自己的MIME映射,它们是FileExtensionContentTypeProvider实现的一部分.该StaticFileMiddleware有一个StaticFileOptions,你可以用它来配置它,当你在初始化Startup.cs.在该选项类中,您可以设置内容类型提供程序.您可以实例化默认内容类型提供程序,然后只需调整映射字典,或者您可以从头开始编写整个映射(不推荐).
如果您要为整个站点提供的扩展文件类型集不会更改,则可以配置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
...
});
...
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1370 次 |
| 最近记录: |