Last post Apr 15, 2014 09:57 AM by FRMJiggs
Apr 11, 2014 01:27 PM|FRMJiggs|LINK
what is the best way of calling following web api method? As you can see here actual return type is IEnumerable<Person> type.
public IHttpActionResult Get()
catch (Exception ex)
Below is the client code but it does not cast in appropriate result with correct type:
var data = default(IEnumerable<T>);
var response = _client.GetAsync(relativeUri).Result;
response.EnsureSuccessStatusCode(); // Throw on error code.
data = response.Content.ReadAsAsync<IEnumerable<T>>().Result; // <--- data variable does not get the values.
//return response status code/reason phrase
Also following method returns the JSON string, but then it needs manual deserializing back to IEnumerable<T>.
Any help to show appropriate client-side method to read response values would be appriciated.
Apr 15, 2014 09:57 AM|FRMJiggs|LINK
I just wanted to update that I have found the root cause of the issue that I was facing.
The code sample that I mentioned is actually the correct way of calling web api methods.
In my case, JSON string was formatted in Camel Casing at server side and at client side, default formtting was used.
this is server side code which was causing the issue:
var jsonFormatter = config.Formatters.OfType<JsonMediaTypeFormatter>().First();
jsonFormatter.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver();
As soon as this was removed, it started working.
Clearly, ReadAsAsync method could not find the matching properties and therefore all values were being dropped.