网络 api 响应 header 中不存在 Etag

Etag is not present in web api response header

在 Web API 响应中,我试图在 Web api 的操作中设置 Etag Header。但它不存在于响应中 header.

我无法使用 ActionFilter 属性设置 ETag。我必须将其付诸实践。

        [CacheControl(MaxAge = 5)]
        public HttpResponseMessage GetStreamByPath(int presId)
        {
            HttpResponseMessage response = new HttpResponseMessage();

            // Some Code here
            //testStream is obj.

            if (testStream != null)
            {
                var getEatg = ETagHelper.GetETag(testStream.LastModifiedDate.ToString());

                if (Request.Headers.Contains("If-None-Match") && Request.Headers.GetValues("If-None-Match").ToString() == getEatg)
                {
                    return Request.CreateResponse(HttpStatusCode.NotModified);
                }

                response.Headers.ETag = new EntityTagHeaderValue(String.Format("\"{0}\"", getEatg));

                response = new HttpResponseMessage(HttpStatusCode.OK) { Content = new StreamContent(testStream.Stream) };
                response.Content.Headers.Add("content-type", " image/jpeg");
            }
            else
            {
                response = new HttpResponseMessage(HttpStatusCode.Unauthorized);
            }

            return response;
        }

您可以像下面这样设置您的header:

HttpContext.Current.Response.Headers.Add("ETag", String.Format("\"{0}\"", getEatg));