使用HttpClient和Odatav4缓存和解析etag(Etag在Httpclient中始终为null)

时间:2016-07-21 09:54:17

标签: c# http asp.net-web-api http-headers odata

案例:我正在使用EF6和WebApi控件来存储我的结果

    public class Profile
{
    public string ZipFile { get; set; } // input parameter
    [ConcurrencyCheck]
    public Guid Id { get; set; } // output parameter generated by Web API

    public string Name { get; set; } // output parameter from Profile XML file

    public string Description { get; set; } // output parameter from Profile XML file}

所以我使用ConcurrencyCheck生成带有odataV4的etag

当我正在阅读获得结果的帖子我得到: enter image description here

正如你在那里看到的那样

问题:在我的客户端,我使用HttpClient,如下所示

 public static string GetAsJsonAsync(string route)
    {
        string result = string.Empty;

        try
        {

            var fullRoute = PrepareHttpRequest(route);
            var client = new HttpClient();
            using (HttpResponseMessage response = client.GetAsync(fullRoute).Result)
            {
                result = response.Content.ReadAsStringAsync().Result;
                response.EnsureSuccessStatusCode();
            }
        }
        catch (Exception ex)
        {
            throw new HttpRequestException(result, ex);
        }

        return result;


    }

响应标头if-match和etag总是,我不知道如何解析“@ odata.etg”值, 所以我的问题是我如何解析etag值并存储它?

1 个答案:

答案 0 :(得分:0)

对于将来的参考,解决方案是附加到您的模型[JsonProperty("@odata.etag")]

相关问题