简体   繁体   中英

Etag is not present in web api response header

In Web API response, I'm trying to set Etag Header inside action of web api. but it's not present in response header.

I can not set ETag using ActionFilter Attribute. I have to set it in action.

        [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;
        }

您可以如下设置标题:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM