0

I created (with some help from here) my RESTful API service. I works well enough but the returning data is in XML format and I want it to be in JSON instead. It's an array of data. The methods to retrieve the data are either:

   public IEnumerable<Photo> GetAllPhotos()
    {
        return photos;

    }

or

    public IHttpActionResult GetPhotoById(int PhotoId)
    {

        var photos= Array.FindAll(photos, x => x.PhotoId== PhotoId);
        if (photos== null)
        {
            return NotFound();
        }
        return Ok(photos);
    }

But one requirement is to return the data as JSON.

0

2 Answers 2

3

This is happening because your browser sends text/xml in its Accept header, and Web API supports content negotiation.

To remove the XML formatter, add this to your application's startup code:

var configuration = GlobalConfiguration.Configuration;

configuration.Formatters.Remove(configuration.Formatters.XmlFormatter);
Sign up to request clarification or add additional context in comments.

2 Comments

Using .NET 4.5.2 GlobalConfiguration is not in System.Web.Http. And if I want to give users the choice of XML or Json?
If you want to give the users a choice of XML or JSON, don't do anything. Just send a request with "Accept: application/json" or "Accept: text/xml" - and don't use a browser to send test requests.
0

Naturally, web browsers want to receive data in a format that they can best understand and work with. Generally, this means text/html

You can remove the JSON formatter or the XML formatter from the list of formatters, if you do not want to use them. The main reasons to do this are:

To restrict your web API responses to a particular media type. For example, you might decide to support only JSON responses, and remove the XML formatter.

The following code shows how to remove the default formatters. Call this from your Application_Start method, defined in Global.asax.

void ConfigureApi(HttpConfiguration config)
{
    // Remove the JSON formatter
    config.Formatters.Remove(config.Formatters.JsonFormatter);

    // or

    // Remove the XML formatter
    config.Formatters.Remove(config.Formatters.XmlFormatter);
}

Honestly, when you really distill it down, the best answer is don't use a browser to test your APIs. Why? Because this isn't what browsers do best, and there are a ton of tools that allow you to test APIs much, much easier. And they're all free. And you should be using them because testing APIs in a browser (outside of plugins or the inspector) is a fool's game.

source http://www.asp.net/web-api/overview/formats-and-model-binding/json-and-xml-serialization

3 Comments

OK, that worked. (Returning data in JSON format). But how do I expose the metadata from my service so it can be discovered by other applications?
Please disregard my last comment. I figured it all out.
thank you . please mark answer if it worked for you .. that inspire me

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.