Asp.Net Core 自定义设置Http缓存处理
2018-05-16 17:52
1026 查看
一、使用中间件 拦截请求自定义输出文件
输出前自定义指定响应头
public class OuterImgMiddleware { public static string RootPath { get; set; } //配置文件读取绝对位置 private readonly RequestDelegate _next; public OuterImgMiddleware(RequestDelegate next, IHostingEnvironment env) { _next = next; } public async Task Invoke(HttpContext context) { var path = context.Request.Path.ToString(); var headersDictionary = context.Request.Headers; if (context.Request.Method == "GET") if (!string.IsNullOrEmpty(path) && path.Contains("/upload/logo")) { var unauthorizedImagePath = RootPath + path; context.Response.ContentType = "image/jpeg"; context.Response.Headers["Cache-Control"] = "public"; //指定客户端,服务器都处理缓存 int length = path.LastIndexOf(".") - path.LastIndexOf("/") - 1; context.Response.Headers["Etag"] = path.Substring(path.LastIndexOf("/") + 1, length); context.Response.Headers["Last-Modified"] = new DateTime(2018).ToString("r"); FileInfo file = new FileInfo(unauthorizedImagePath); context.Response.Headers["Content-Length"] = file.Length.ToString(); context.Response.Headers["Accept-Ranges"] = "bytes"; //如果Transfer-Encoding =chunked 没有指定的话,SendFile 前台解析事变 //指定Content-Length 可以是 chunked 分块实效 //context.Response.Headers["Transfer-Encoding"] = ""; //Http 1.0 是使用Expires 属性 //context.Response.Headers["Expires"] = DateTime.Now.AddMonths(1).ToString("r"); await context.Response.SendFileAsync(unauthorizedImagePath); return; } await _next(context); } } public static class MvcExtensions { public static IApplicationBuilder UseOutImg(this IApplicationBuilder builder) { return builder.UseMiddleware<OuterImgMiddleware>(); } }
更多:
Http缓存机制(转)
分块编码(Transfer-Encoding: chunked)(转)
相关文章推荐
- ASP.Net中自定义Http处理及应用之HttpModule篇
- ASP.Net中自定义Http处理及其应用(转:孙亚民)
- C#编译器优化那点事 c# 如果一个对象的值为null,那么它调用扩展方法时为甚么不报错 webAPI 控制器(Controller)太多怎么办? .NET MVC项目设置包含Areas中的页面为默认启动页 (五)Net Core使用静态文件 学习ASP.NET Core Razor 编程系列八——并发处理
- ASP.NET 之 自定义 异步HTTP处理程序
- ASP.Net中自定义Http处理及应用之HttpModule篇
- ASP.Net中自定义Http处理及应用之HttpHandler
- ASP.Net中自定义Http处理及应用之HttpModule篇
- ASP.Net Core2.1中的HttpClientFactory系列二:集成Polly处理瞬态故障
- ASP.NET之自定义同步HTTP处理程序(图文教程)
- ASP.Net中自定义Http处理及应用之HttpHandler篇
- ASP.Net中自定义Http处理及应用之HttpHandler篇
- [导入]ASP.Net中自定义Http处理及应用之HttpModule篇
- ASP.Net中自定义Http处理及应用之HttpHandler篇(转自:http://7880.com/Info/Article-2b783c20.html)
- ASP.Net中自定义Http处理及应用之HttpHandler篇
- ASP.Net中自定义Http处理及应用之HttpHandler篇
- ASP.Net中自定义Http处理及应用之HttpHandler篇
- ASP.Net中自定义Http处理及应用之HttpModule篇
- ASP.Net中自定义Http处理及应用之HttpModule篇
- ASP.Net中自定义Http处理及应用之HttpModule篇
- ASP.Net中自定义Http处理及应用