0
votes

My simple Action method below always returns JSON, regardless of having Accept header set to application/xml. Content negotiation works fine on other actions I have in this same controller.

public HttpResponseMessage GetOrder(int id) {
    var orderDescription = mydbc.tbl_job_versions.AsNoTracking().Where(t => t.JobId == id)
      .Select(t => new{Id = t.JobId,  Description = t.Brand + " " + t.Variety + " " + t.Promotion  + " " + t.MarketSegment }).FirstOrDefault ();

    if (orderDescription == null) {
      return new HttpResponseMessage(HttpStatusCode.NotFound);
    }
    else {
      return Request.CreateResponse((HttpStatusCode)200, orderDescription);
    }
 }

What might be causing this to not perform content negotiation and instead always return JSON?

2

2 Answers

0
votes

You can check my answer here : https://stackoverflow.com/a/22918118/1184056

Looks like the xml formatter is unable to say that it can write the object that you are giving to it and so you are not seeing the response in json.

0
votes

Discovered that the WEB API content negotiation cannot handle serializing anonymous types to XML, only JSON. If I create a class, populate it from the LINQ data and return it I get XML when asking for XML.

Looking around some more I found the issue on codeplex http://aspnetwebstack.codeplex.com/workitem/2123