.net HttpClient with custom JsonConverter

前端 未结 3 898
离开以前
离开以前 2020-12-16 14:14

I\'m using .NET\'s HttpClient to make requests to a WebAPI that returns some JSON data that requires a little bit of custom deserialization on the client\'s side. For this

3条回答
  •  夕颜
    夕颜 (楼主)
    2020-12-16 14:48

    I was able to add a custom JsonConverter to the default formatters for HttpClient with the following:

    MediaTypeFormatterCollection formatters = new MediaTypeFormatterCollection(); 
    formatters.JsonFormatter.SerializerSettings.Converters.Add(new MyCustomConverter());
    
    
    var result = response.Content.ReadAsAsync(formatters).Result;
    

    This seemed to allow you to just add your custom converter to the default converters.

提交回复
热议问题