Solved

Web API in WebForms app returning XML instead of JSON

Posted on 2013-05-28
1
1,399 Views
Last Modified: 2013-05-30
I added Web API to my existing Web Forms (NOT MVC) ASP.NET application using these instructions:

http://blogs.msdn.com/b/henrikn/archive/2012/02/23/using-asp-net-web-api-with-asp-net-web-forms.aspx

http://dotnet.dzone.com/articles/web-api-aspnet-web-forms

But, they return XML.  
<ArrayOfstring xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://schemas.microsoft.com/2003/10/Serialization/Arrays">
   <string>value1</string>
   <string>value2</string>
</ArrayOfstring>

Open in new window


I tried using JSON.NET...
    public string Get(int id)
    {
        DataTable person = MemberFunctions.GetContactInfo(id);
        return JsonConvert.SerializeObject(person);   
    }

Open in new window


and it generates the JSON I need but it is still wrapped in XML tags:
<string xmlns="http://schemas.microsoft.com/2003/10/Serialization/">
    [{"Prefix":"Dr","FirstName":"Josh",   ....etc.......   }]
</string>

Open in new window

How can I get WebAPI in a WebForms app to return valid JSON instead of XML, preferably using JSON.NET?
0
Comment
Question by:joshbula
1 Comment
 
LVL 9

Accepted Solution

by:
joshbula earned 0 total points
Comment Utility
I ended up just removing the XmlFormatter by putting this in the Global.asax Application_Start (this is an existing WebForms application, originally written in VB, so the Global.asax is VB even though all my new classes and code are C#.)
        Dim formatters = GlobalConfiguration.Configuration.Formatters
        formatters.Remove(formatters.XmlFormatter)

Open in new window

But it was still adding quotes and escaping all the quotes, so I realized that it had to return a JObject, not a string, and then Parse it using JObject.Parse:
  public JObject Get(int id)
    {
        DataRow person = MemberFunctions.GetContactInfo(id).Rows[0];
        JObject output = JObject.Parse(JsonConvert.SerializeObject(person));
        return output;
    }

Open in new window

This requires using Newtonsoft.Json.Linq;

To convert an entire datatable or dataset, I ended up just using JsonConvert.SerializeObject without parsing it into a JObject:
string output = JsonConvert.SerializeObject(dataSet, Formatting.None);

Open in new window

and then a Response.Write() and Response.End() to prevent it from adding extra quotes and escaping stuff.
Httpcontext.Current.Response.Write(output);
HttpContext.Current.Response.End();

Open in new window

I'm not sure if that's the best way to do that, but it seems to be working.
0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

I found this questions asking how to do this in many different forums, so I will describe here how to implement a solution using PHP and AJAX. The logical flow for the problem should be: Write an event handler for the first drop down box to get …
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now