DbDataController content negotiation failshttp://forums.asp.net/t/1773169.aspx/1?DbDataController+content+negotiation+failsMon, 12 Mar 2012 21:36:01 -040017731694848011http://forums.asp.net/p/1773169/4848011.aspx/1?DbDataController+content+negotiation+failsDbDataController content negotiation fails <p>Even if I askes specifically for xml, it keeps giving me back json:</p> <pre class="prettyprint">public class DataServiceController : DbDataController&lt;ListItCheckItContext&gt; { public IQueryable&lt;Category&gt; GetCategories() { return DbContext.Categories.AsQueryable(); } }</pre> <p>this is the request:</p> <p><em>GET&nbsp;http://localhost:10914/api/DataService/GetItems</em></p> <p><em>User-Agent: Fiddler</em><br> <em>Accept: application/xml</em><br> <em>Host: localhost:10914</em></p> <p>And this is the response:</p> <p><em>HTTP/1.1 200 OK</em><br> <em>Cache-Control: no-cache</em><br> <em>Pragma: no-cache</em><br> <em>Transfer-Encoding: chunked</em><br> <em>Content-Type: application/json; charset=utf-8</em><br> <em>Expires: -1</em><br> <em>Server: Microsoft-IIS/7.5</em><br> <em>X-AspNet-Version: 4.0.30319</em><br> <em>X-SourceFiles: =?UTF-8?B?RDpcRHJvcGJveFxBcHBzXEFTUE5FVE1WQ1NhbXBsZXNcc3JjXExpc3RJdENoZWNrSXRcTGlzdEl0Q2hlY2tJdFxhcGlcRGF0YVNlcnZpY2VcR2V0SXRlbXM=?=</em><br> <em>X-Powered-By: ASP.NET</em><br> <em>Date: Thu, 23 Feb 2012 21:06:29 GMT</em></p> <p><em>65</em><br> <em>[{&quot;Category&quot;:null,&quot;CategoryId&quot;:1,&quot;CompletedOn&quot;:null,&quot;Id&quot;:1,&quot;IsCompleted&quot;:false,&quot;Task&quot;:&quot;Check this!&quot;}]</em><br> <em>0</em></p> <p></p> 2012-02-23T20:07:24-05:004861908http://forums.asp.net/p/1773169/4861908.aspx/1?Re+DbDataController+content+negotiation+failsRe: DbDataController content negotiation fails <p></p> <blockquote><span class="icon-blockquote"></span> <h4>tugberk_ugurlu_</h4> <a title="tugberk_ugurlu_" href="/members/tugberk_ugurlu_.aspx">tugberk_ugurlu_</a></blockquote> <p></p> <p>Because the AbDataController override some of those feature. If you would return xml use <a href="http://localhost:51955/api/Task">APIController.</a></p> <p><strong>MVC DataController uses DataContractJsonSerilizer which requires models contain no Cycles</strong></p> 2012-03-03T09:14:03-05:004876193http://forums.asp.net/p/1773169/4876193.aspx/1?Re+DbDataController+content+negotiation+failsRe: DbDataController content negotiation fails <p>What does the Category type look like?</p> <p>One possible explanation is that the XmlMediaTypeFormatter cannot serialize IQueryable&lt;Category&gt;.</p> <p>By default, XmlMediaTypeFormatter uses XmlSerializer, but can be modified to use DataContractSerializer (config.Formatters.XmlFormatter.UseDataContractSerializer).&nbsp;&nbsp; Bing &quot;XmlSerializer&quot; for serialization issues to see if they sound familiar.</p> <p>Alternatively, try using the DataContractSerializer as described above.</p> <p>The content negotiation algorithm tries for a best match for both *type* and for required media type.&nbsp;&nbsp; If it fails matching the type, it will not use that MediaTypeFormatter, even if the media type matches.&nbsp; In your case, the JsonMediaTypeFormatter is probably saying &quot;yes&quot; to the Category type, and the XmlMediaTypeFormatter is probably saying &quot;no&quot;.&nbsp; If only the type matches, that MediaTypeFormatter is used, regardless what you had in your Accept header.</p> <p>Does that sound like a possibility for your object hierarchy?</p> 2012-03-12T20:51:51-04:004876274http://forums.asp.net/p/1773169/4876274.aspx/1?Re+DbDataController+content+negotiation+failsRe: DbDataController content negotiation fails <p>Incidentally, I'm curious what others think of the content negotiation decision that &quot;type is good enough if I can't match media type too&quot;.</p> <p>Given your choice, what kind of response would you prefer to see?</p> <p>BadRequest?&nbsp;&nbsp; It's really an issue with the types you are returning from the DbDataController/ApiController's actions.</p> <p>InternalServerError?&nbsp;&nbsp; Those are always a joy to debug.</p> <p>Incidentally, if you think returning a different media type based on a type-only match is unacceptable, you can replace the IFormatterSelector in the HttpConfiguration.ServiceResolver with one of your own.&nbsp; It can subclass the default FormatterSelector but 2nd guess the choice -- or throw an HttpResponseException with an HttpResponse of your choice.</p> 2012-03-12T21:36:01-04:00