asp.net 响应标头中不存在Etag

5sxhfpxr  于 2023-05-30  发布在  .NET
关注(0)|答案(1)|浏览(194)

我尝试在响应头中设置Etag,但当我在浏览器中检查响应头时,我看不到它。顺便说一下,这只适用于Etag,我可以看到另一个标签和值。顺便说一句,我可以在本地主机上看到它,但当我发布代码到服务器时,我看不到。

public static void InsertETag(HttpResponseBase response, Model model)
  {
  

   try
   {
  
    var eTagValue = CalculateETag(model);
    if (!string.IsNullOrEmpty(eTagValue))
    {
   
     
     var matchETagValue = HttpContext.Current.Request.Headers["If-None-Match"];
     if (matchETagValue != null && matchETagValue == eTagValue)
     {
      response.SuppressContent = true;
      response.StatusCode = 304;
      response.StatusDescription = "Not Modified";
      response.Headers["Content-Length"] = "0";
      response.AppendHeader("ETag", eTagValue);
     }
     else
     {
      response.AppendHeader("ETag", eTagValue);
     }
    }
   }
  }
y53ybaqx

y53ybaqx1#

从这里How to use ETag in Web API using action filter along with HttpResponseMessage

var request = context.Request;
        var key = GetKey(request);

        EntityTagHeaderValue etag;
        if (!etags.TryGetValue(key, out etag) || request.Method == HttpMethod.Put || request.Method == HttpMethod.Post)
        {
            etag = new EntityTagHeaderValue("\"" + Guid.NewGuid().ToString() + "\"");
            etags.AddOrUpdate(key, etag, (k, val) => etag);
        }

        context.Response.Headers.ETag = etag;
        SetCacheControl(context.Response);

相关问题