Solved

Web API in WebForms app returning XML instead of JSON

Posted on 2013-05-28
1
1,523 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
ID: 39207732
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

How our DevOps Teams Maximize Uptime

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us. Read the use case whitepaper.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction A frequently asked question goes something like this:  "I am running a long process in the background and I want to alert my client when the process finishes.  How can I send a message to the browser?"  Unfortunately, the short answer …
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

861 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