Solved

Web API in WebForms app returning XML instead of JSON

Posted on 2013-05-28
1
1,493 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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

816 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

14 Experts available now in Live!

Get 1:1 Help Now