Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to force ASP.NET Web API to return JSON or XML data based on my input?

I try to get the output XML or JSON data based on my input. I used the below WEB API code but not able to exact output.

public string Get(int id) {     if (GlobalConfiguration.Configuration.Formatters.XmlFormatter == null)     {         GlobalConfiguration.Configuration.Formatters.Add(GlobalConfiguration.Configuration.Formatters.XmlFormatter);     }     if (GlobalConfiguration.Configuration.Formatters.JsonFormatter == null)     {         GlobalConfiguration.Configuration.Formatters.Add(GlobalConfiguration.Configuration.Formatters.JsonFormatter);     }     if (id == 1)     {         GlobalConfiguration.Configuration.Formatters.Remove(GlobalConfiguration.Configuration.Formatters.JsonFormatter);                         GlobalConfiguration.Configuration.Formatters.XmlFormatter.UseXmlSerializer = true;                     }     else     {         GlobalConfiguration.Configuration.Formatters.Remove(GlobalConfiguration.Configuration.Formatters.XmlFormatter);         GlobalConfiguration.Configuration.Formatters.JsonFormatter.UseDataContractJsonSerializer = true;     }     return "value"; } 
like image 365
Rajasekar Gunasekaran Avatar asked Nov 14 '13 10:11

Rajasekar Gunasekaran


People also ask

How do I return XML and JSON from Web API in .NET core?

In order to return XML using an IActionResult method, you should also use the [Produces] attribute, which can be set to “application/xml” at the API Controller level. [Produces("application/xml")] [Route("api/[controller]")] [ApiController] public class LearningResourcesController : ControllerBase { ... }

Can ASP Net Web API specialize to XML or JSON?

Web API provides media-type formatters for both JSON and XML. The framework inserts these formatters into the pipeline by default. Clients can request either JSON or XML in the Accept header of the HTTP request.


2 Answers

Add the below code app_start event in global.asax file. In API Url add the query string:

GlobalConfiguration.Configuration.Formatters.JsonFormatter.MediaTypeMappings.Add(     new QueryStringMapping("type", "json", new MediaTypeHeaderValue("application/json")));  GlobalConfiguration.Configuration.Formatters.XmlFormatter.MediaTypeMappings.Add(     new QueryStringMapping("type", "xml", new MediaTypeHeaderValue("application/xml"))); 

e.g.:

for xml : http://localhost:49533/api/?type=xml  for json: http://localhost:49533/api/?type=json 
like image 177
vijayjan15 Avatar answered Sep 29 '22 01:09

vijayjan15


What you are trying to do will not work in a multi-threaded environment. You cannot add to and remove from the formatters collection on a per-request basis. Here is a better way of accomplishing what you want.

public HttpResponseMessage Get(int id) {     Foo foo = new Foo();     var content = new ObjectContent<Foo>(foo,                     ((id == 1) ? Configuration.Formatters.XmlFormatter :                                 Configuration.Formatters.JsonFormatter));     return new HttpResponseMessage()     {          Content = content     }; } 
like image 43
Badri Avatar answered Sep 28 '22 23:09

Badri