Web API响应标头中不存在Etag

时间:2018-10-10 09:40:35

标签: c# asp.net asp.net-web-api etag

在Web API响应中,我试图在Web api的动作中设置Etag 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;
        }

1 个答案:

答案 0 :(得分:1)

您可以如下设置标题:

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