0

I have a method which requests external webservices and returns json here:

    public string GetJsonRequest(string url)
    {
        string response = string.Empty;
        var request = System.Net.WebRequest.Create(url) as HttpWebRequest;
        if (request != null)
        {
            request.Method = WebRequestMethods.Http.Get;
            request.Timeout = 20000;
            request.ContentType = "application/json";


            var httpresponse = (HttpWebResponse)request.GetResponse();

            using (var streamreader = new StreamReader(httpresponse.GetResponseStream()))
                   response = streamreader.ReadToEnd();

            if (httpresponse != null) httpresponse.Close();
        }
        return response;
    }

And a method which returns the result here:

    public JsonResult Makes()       
    {
        CarRepository rep = new CarRepository();
        return new JsonResult()
        {
            Data = rep.GetMakes(),
            ContentType = "application/json"
        };
    }

or

    public string Makes()       
    {
        CarRepository rep = new CarRepository();
        return rep.GetMakes();
    }

This returns correct json but it is wrapped in XML

<JsonResult xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
   <ContentType>application/json</ContentType>
   <Data xsi:type="xsd:string">
       The JSON data.......
   </Data>
  <JsonRequestBehavior>AllowGet</JsonRequestBehavior>
  <MaxJsonLength xsi:nil="true"/>
  <RecursionLimit xsi:nil="true"/>
</JsonResult>

I have checked the request in fiddler and the Accept headers only have xml values in. How can i get this to just print out json? I am using the ASP.NET Web API. I can remove the XML mediatypeformatter on application start, but i might need to use it later on down the line so i dont think thats the way to go.

Thanks in advance

tpeczek
  • 23,867
  • 3
  • 74
  • 77
gdp
  • 8,032
  • 10
  • 42
  • 63

2 Answers2

2

You shouldn't be returning JsonResult from ApiController action. ApiController actions should return objects (or collections) and the MediaTypeFormatters are taking care of serializng them to the JSON, XML or anything else (based on content type requested). Please take a look at this basic tutorial.

UPDATE

To make sure that client is requesting for JSON (not XML) and the Web API will try to use proper MediaTypeFormatter add this to your client:

request.Accept = "application/json";
tpeczek
  • 23,867
  • 3
  • 74
  • 77
  • Ok i have changed it to return a string - but its still the same. Json wrapped in XML. – gdp Apr 02 '12 at 10:38
  • Are you saying that rep.GetMakes() returns a string? Please check my update about your client request also. – tpeczek Apr 02 '12 at 10:41
  • yes rep.getmakes() returns a string. I dont think i can return an object because the data changes a lot depending on where i get it from. – gdp Apr 02 '12 at 10:49
  • 1
    Yes thats correct i was just being an idiot and loading it from the browser which defaulted to application/xml when i hand crafted the request in fiddler it worked find. A second problem with it though is the JSON string quotes are being escaped? Thankyou for your help so far – gdp Apr 02 '12 at 10:58
  • @tpeczek I have a similar issue.. Can u please check this and give me a solution http://stackoverflow.com/questions/13558856/what-should-be-the-correct-response-from-web-service-to-display-the-jquery-token/13559622#comment18576300_13559622 – Xavier Nov 26 '12 at 06:40
0

Instead of returning a string in your WebMethod use:

JavaScriptSerializer js = new JavaScriptSerializer();
Context.Response.Write(js.Serialize(YOUR_STRING_TO_OUTPUT));
JayC
  • 39
  • 4