2

I am consuming a Odata end point in my web API and trying return JSON response from the API.My controller is like below

public class GetEmployeesDEVController : ApiController
{
    [HttpGet]
    public async Task<EmployeeDTO.RootObject> Get()
    {
        EmployeeDTO.RootObject returnObj = new EmployeeDTO.RootObject();
        var responsedata = "";
        using (var client_Core = new HttpClient())
            {
                ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(delegate { return true; });
                Uri uri = new Uri(BaseURL_Core);
                client_Core.BaseAddress = uri;
                client_Core.DefaultRequestHeaders.Accept.Clear();
                client_Core.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
                client_Core.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Basic", Convert.ToBase64String(byteArray_Core));
                string core_URL = BaseURL_Core;
                var response = client_Core.GetAsync(core_URL).Result;
                responsedata = await response.Content.ReadAsStringAsync();
      returnObj = JsonConvert.DeserializeObject<EmployeeDTO.RootObject>(responsedata);
        return returnObj;
    }

Here EmployeeDTO is the model class for the Odata response. When I call my API it returns nulls and dont return any data. But the responsedata holds JSON that has data. Screenshot of responsedata and returnObj is below

enter image description here enter image description here

I am not sure if I am missing anything here. All I need to get from my API is the exact response from the Odata endpoint that is stored in responsedata

4
  • Shouldn’t you deserialize the object instead of serializing it? Commented Aug 18, 2018 at 8:15
  • @OrElse Yes I am Deserializing it was mistake when I pasted as I was trying to troubleshoot the issue. I have the above behaviour while deserializing Commented Aug 18, 2018 at 8:17
  • In your case, I would separate the issues like pasting as classes the responsedata in visual studio and then trying to deserialize on it. Commented Aug 18, 2018 at 8:28
  • Check this line : client_Core.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); you will get json value in responsedata then why you are trying to convert in json again, just return responsedata; Commented Aug 18, 2018 at 9:06

1 Answer 1

2

add to your global.asax

GlobalConfiguration.Configuration.Formatters.JsonFormatter.SerializerSetting=
new JsonSerializerSettings
{
    Formatting = Formatting.Indented,
    TypeNameHandling = TypeNameHandling.Objects,
    ContractResolver = new CamelCasePropertyNamesContractResolver()
};
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.