-1

I know this question is been discussed many times but for me those solutions are not working. I want to return JSON data from my ASP.NET web API. I am hitting the end point using Firefox REST client plugin.

What I have tried:

  1. I have specific accept header : Accept: application/json. Use accept header

  2. Removed the XML formatter on Application_Start method

    var formatters = GlobalConfiguration.Configuration.Formatters;

    formatters.Remove(formatters.XmlFormatter);

This is how I return data at the end

 return myModel.OrderBy(d => d.SortOrder);

Where myModel is just a class with few public property. I am not decorating this class or its property with any attribute.

But these two approach's are not working. I am still getting data in XML format :(

Please provide your suggestions.

Community
  • 1
  • 1
SharpCoder
  • 18,279
  • 43
  • 153
  • 249
  • Show us your code, we cannot answer based on this. – Roy Dictus Sep 17 '13 at 09:57
  • @RoyDictus: Thank you for quick reply. I cannot share the code in this forum :(. I think getting data back in JSON format should depend on some configuration setting. I am hoping if someone can point out or suggest some config changes which will help me getting the result. – SharpCoder Sep 17 '13 at 10:02
  • Are you returning the entity directly from the api and not a string result etc etc? – Slicksim Sep 17 '13 at 10:10
  • @Slicksim: This is what I return :- return myModel.OrderBy(d => d.SortOrder); Where myModel is just a class with few public property. I am not decorating this class with any attribute. – SharpCoder Sep 17 '13 at 10:13
  • what is the return type of your method? the orderby will return an Iorderable does it not? – Slicksim Sep 17 '13 at 10:19
  • @RoyDictus Mock up a simple application using dummy class names with your current implementation and share that. – Bronumski Sep 17 '13 at 10:26
  • Are the properties in the Model just simple properties? If I recall correctly there are some types that throw away JSON serialization... Also, check that the `application/json` header is the first header and has a `q=1` quality attribute: "application/json;q=1"... just in case you – Tallmaris Sep 17 '13 at 10:36
  • Also, worth noting, is the accept header set on the **client**? (that is, the firefox rest client plugin?) – Tallmaris Sep 17 '13 at 10:40
  • @Slicksim: I am returning a simple class called myModel which contains few property which are either string, int or dateTime type. – SharpCoder Sep 17 '13 at 11:02
  • @Tallmaris: Yes, I have set the accept type on the header. – SharpCoder Sep 17 '13 at 11:03
  • @Tallmaris: setting the quality attribute "q=1" in the accept header is doing the trick. Please add this as answer and I will mark the solution as answer. If possible please explain why adding q=1 is returning data in JSON format & when I have explicitly removed the XML format in the code, why I was getting the data in XML format? Thank you for the help – SharpCoder Sep 17 '13 at 11:14

2 Answers2

1

I would like to introduce you to http://www.servicestack.net/

This is rest API framework that hooks up with .net.

It does everything what you require . https://docs.google.com/presentation/d/16ey0MrpHOSz5N5sjctAliOgYWO3ZYeJe070fLZlPdrE/present#slide=id.i27

Nipun Ambastha
  • 2,553
  • 1
  • 16
  • 26
0

FROM COMMENT

Check that the application/json header is the first header and has a q=1 quality attribute: "application/json;q=1".

You can read more about quality attributes in the specs. Basically they are a way for a client of providing a preference system on the returned datatype.

To answer your other question (when I have explicitly removed the XML format in the code, why I was getting the data in XML format?), I can only guess what was going on: either the header was not setup correctly or the client was defaulting the other header to a different quality value.

Another guess could be that your way of removing the formatter is wrong: you can check this answer on SO or this article for alternative methods and see if they do the trick for you as well.

Community
  • 1
  • 1
Tallmaris
  • 7,605
  • 3
  • 28
  • 58