【问题标题】:Etag is not present in web api response headerWeb api 响应标头中不存在 Etag
【发布时间】:2019-03-15 04:25:24
【问题描述】:

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

【问题讨论】:

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


    【解决方案1】:

    你可以像下面这样设置你的标题:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-12-18
      • 2014-03-21
      • 2013-12-24
      • 2014-01-07
      • 1970-01-01
      • 2011-09-21
      • 1970-01-01
      相关资源
      最近更新 更多