public class HttpCacheModule:IHttpModule { public void Init(HttpApplication context) { context.EndRequest += context_EndRequest; } void context_EndRequest(object sender, EventArgs e) { HttpApplication app = (HttpApplication)sender; IServiceProvider provider = (IServiceProvider)app.Context; HttpWorkerRequest worker = (HttpWorkerRequest)provider.GetService(typeof(HttpWorkerRequest)); //设置响应头 app.Context.Response.Cache.SetCacheability(HttpCacheability.Public); //worker.SendKnownResponseHeader(HttpWorkerRequest.HeaderCacheControl, "public"); worker.SendKnownResponseHeader(HttpWorkerRequest.HeaderExpires, Convert.ToDateTime(Common.Common.currDateTime).AddHours(1).ToUniversalTime().ToString("r")); worker.SendKnownResponseHeader(HttpWorkerRequest.HeaderLastModified, Convert.ToDateTime(Common.Common.currDateTime).ToUniversalTime().ToString("r")); app.Context.Response.Write("来自HttpModule的你。"); } }
请求页面总是返回200,这样实现HTTP缓存协商哪里不对呢?
第一次请求:
再次请求:
请高人帮我解惑,谢谢。
你想返回304?
app.Context.Response.Cache.SetMaxAge(new TimeSpan(DateTime.Now.AddYears(1).Ticks));
app.Context.Response.Cache.SetExpires(DateTime.Now.AddYears(1));
app.Context.Response.AddHeader("Expires", DateTime.Now.AddYears(1).ToShortDateString());