首页 新闻 会员 周边 捐助

用HttpModule实现HTTP缓存协商不行吗?

0
悬赏园豆:5 [已解决问题] 解决于 2017-11-22 09:02

复制代码
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缓存协商哪里不对呢?

第一次请求:

再次请求:

请高人帮我解惑,谢谢。

skybirdzw的主页 skybirdzw | 初学一级 | 园豆:3
提问于:2017-11-15 13:57
< >
分享
最佳答案
0

你想返回304?

收获园豆:5
dudu | 高人七级 |园豆:30778 | 2017-11-15 15:57
其他回答(1)
0

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());

Daniel Cai | 园豆:10424 (专家六级) | 2017-11-15 17:20
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册