Programmer
Programmer

Reputation: 418

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

Upvotes: 0

Views: 1056

Answers (1)

Amit Kumar
Amit Kumar

Reputation: 5962

You can set your header like below:

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

Upvotes: 1

Related Questions