在ASP.net 4.5中,我们过去可以通过在web.config中添加“ClientCache”来启用静态资源上的过期标头(切换,启用浏览器缓存),如下所示:
<staticcontent>
<clientcache cachecontrolmode="UseMaxAge" cachecontrolmaxage="365.00:00:00" />
</staticcontent>如http://madskristensen.net/post/cache-busting-in-aspnet中所引用的
在ASP.net 5中,当我们没有web.config和Startup.cs时,我们如何做到这一点呢?
发布于 2015-12-02 07:14:57
在Startup.cs >配置中(IApplicationBuilder applicationBuilder,.)
applicationBuilder.UseStaticFiles(new StaticFileOptions
{
OnPrepareResponse = context =>
context.Context.Response.Headers.Add("Cache-Control", "public, max-age=2592000")
});发布于 2015-08-21 17:01:58
如果您正在使用MVC,您可以在操作中使用ResponseCacheAttribute来设置客户端缓存头。还有一个可以使用的ResponseCacheFilter。
发布于 2015-09-01 18:58:55
你用哪台服务器?
中间件:
未测试(!)就在我的头上,,你可以写这样的东西:
public sealed class CacheControlMiddleWare
{
readonly RequestDelegate _next;
public CacheControlMiddleWare(RequestDelegate next)
{
_next = next;
}
public async Task Invoke(HttpContext context)
{
if (context.Request.Path.Value.EndsWith(".jpg")
{
context.Response.Headers.Add("Cache-Control", new[]{"max-age=100"});
}
await _next(context);
}
}nginx作为反向代理:
http://mjomaa.com/computer-science/frameworks/asp-net-mvc/141-how-to-combine-nginx-kestrel-for-production-part-i-installation
除此之外,我还为响应缓存编写了一些说明:
http://mjomaa.com/computer-science/frameworks/asp-net-mvc/153-output-response-caching-in-asp-net-5
https://stackoverflow.com/questions/32138286
复制相似问题