asp.netCore中给动态请求路径加客户端缓存
今天在处理一个图片显示的动态请求路径,为了缓解宽带压力,故增加客户端缓存。
代码很简单如下:
Response.GetTypedHeaders().CacheControl = new CacheControlHeaderValue { Public = true, MaxAge = TimeSpan.FromDays(30) }; //Response.Headers["Cache-Control"] = "public,max-age=2592000"; // 30 天 = 2592000秒
//Response.Headers[Microsoft.Net.Http.Headers.HeaderNames.CacheControl] = "public,max-age=2592000";
var result = PhysicalFile(newFilePath, MimeUtility.GetMimeMapping(newFilePath)); result.EnableRangeProcessing = true; return result;
注意在Visual Studio中调试,注意:如果使用的是 IIS Express有时候不会呈现缓存的效果,IIS Express会处理掉了,也就是返回不会出现 Cache-Control这个关键字,建议使用 Kestrel。
浙公网安备 33010602011771号