首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用ETag添加IHttpModule

使用ETag添加IHttpModule
EN

Stack Overflow用户
提问于 2014-06-29 19:23:33
回答 1查看 240关注 0票数 1

我写了一个简单的IHttpModule

代码语言:javascript
复制
void context_PreSendRequestHeaders(object sender, EventArgs e)
{
    //remove default
    HttpContext.Current.Response.Headers.Remove("ETag");

    //add version one
    HttpContext.Current.Response.Headers.Add("ETag", "Test1.0");
}

如果我想删除IIS,并添加自己的ETag来控制来自客户端的javascript和css文件请求--就像我想要的更新一样,它将被自动刷新。客户端对ETag的响应正常

如果-无-匹配:Test1.0if-修改-自:星期一,2014年6月2日11:08:54格林尼治时间

但是IIS总是返回内容而不是304

EN

回答 1

Stack Overflow用户

发布于 2014-06-29 20:07:43

代码语言:javascript
复制
void context_PreSendRequestHeaders(object sender, EventArgs e)
    {
        var etag = "Test4-0";

        //remove default
        HttpContext.Current.Response.Headers.Remove("ETag");

        //add version one
        HttpContext.Current.Response.Headers.Add("ETag", etag);

        string ifNoneMatch = HttpContext.Current.Request.Headers["If-None-Match"];
        Debug.WriteLine(String.Format("ifNoneMatch - {0}", ifNoneMatch));

        if (ifNoneMatch != null && ifNoneMatch.Contains(","))
        {
            ifNoneMatch = ifNoneMatch.Substring(0, ifNoneMatch.IndexOf(",", StringComparison.Ordinal));
        }

        HttpContext.Current.Response.Cache.VaryByHeaders["If-None-Match"] = true;
        Debug.WriteLine(String.Format("ifNoneMatch - etag: {0}-{1}", ifNoneMatch, etag));
        if (etag == ifNoneMatch)
        {
            Debug.WriteLine(String.Format("ifNoneMatch2 - etag: {0}-{1}", ifNoneMatch, etag));
            HttpContext.Current.Response.ClearContent();
            HttpContext.Current.Response.StatusCode = (int)HttpStatusCode.NotModified;
            HttpContext.Current.Response.SuppressContent = true;
        }
    }
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/24479430

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档