2

I would like to create a web application that returns data in the form of XML or JSON, how do I go about doing this?

The model:

namespace ReturningJSONandXML.Models
{
    public class SomeImportantInformation
    {
        public int ID { get; set; }
        public string Information { get; set; }
    }
}

The controller:

namespace ReturningJSONandXML.Controllers
{
    public class GetInfoController : Controller
    {
        // GET: /<controller>/
        public List<SomeImportantInformation> Get()
        {
            List<SomeImportantInformation> ImportantInfo = new List<SomeImportantInformation>();
            ImportantInfo.Add(new SomeImportantInformation { ID = 0, Information = "Awesome info" });
            ImportantInfo.Add(new SomeImportantInformation { ID = 1, Information = "Some other interesting info" });
            return ImportantInfo;
        }
    }
}

I would like to return an XML and JSON file...

What are the best practice's I should be using here?

2
  • 4
    the api will convert the response to whatever you put on your request header... application/json or application/xml Commented Jun 5, 2017 at 14:30
  • 3
    If you are using MVC 6 (ASP.NET Core) the framework will automatically negotiate the content (returning the appropriate content-type as @NicoRiff said). More info inside the documentation. Commented Jun 5, 2017 at 14:33

3 Answers 3

5

The framework takes care of this automatically for you so that you don't have to reinvent the wheel. The answer is quoted below.

But to make it simpler: Unless you specify an Accept header, the API will serialize the response as JSON. If you specify, for example, 'application/xml' it will return XML.

As MSDN Says:

Content negotiation (conneg for short) occurs when the client specifies an Accept header. The default format used by ASP.NET Core MVC is JSON. Content negotiation is implemented by ObjectResult. It's also built into the status code specific action results returned from the helper methods (which are all based on ObjectResult). You can also return a model type (a class you've defined as your data transfer type) and the framework will automatically wrap it in an ObjectResult for you.

...

Content negotiation only takes place if an Accept header appears in the request. When a request contains an accept header, the framework will enumerate the media types in the accept header in preference order and will try to find a formatter that can produce a response in one of the formats specified by the accept header. In case no formatter is found that can satisfy the client's request, the framework will try to find the first formatter that can produce a response (unless the developer has configured the option on MvcOptions to return 406 Not Acceptable instead). If the request specifies XML, but the XML formatter has not been configured, then the JSON formatter will be used. More generally, if no formatter is configured that can provide the requested format, then the first formatter that can format the object is used. If no header is given, the first formatter that can handle the object to be returned will be used to serialize the response. In this case, there isn't any negotiation taking place - the server is determining what format it will use.

From MSDN -- Current version and version at time of quote.

Sign up to request clarification or add additional context in comments.

Comments

2

In ASP.Net Core 2.0, you can use an even shorter syntax.

In the Startup class' ConfigureServices method, you should have:

services
    .AddMvc()
    .AddXmlSerializerFormatters();

And the controller that accepts complex objects looks like this:

[Route("api/Documents")]
public class DocumentsController : Controller
{
    [Route("SendDocument")]
    [HttpPost]
    public ActionResult SendDocument([FromBody]DocumentDto document)
    {
        return Ok();
    }
}

This is the XML it would accept:

<document>
    <id>123456</id>
    <content>This is document that I posted...</content>
    <author>Michał Białecki</author>
    <links>
        <link>2345</link>
        <link>5678</link>
    </links>
</document>

And the same as JSON:

{
    id: "1234",
    content: "This is document that I posted...",
    author: "Michał Białecki",
    links: {
        link: ["1234", "5678"]
    }
}

And that's it! Sending the same document either in XML or JSON to api/documents/SendDocument endpoint will just work. Just remember to include the correct Content-Type header in your request.

For more content, you can read the whole post at my blog: http://www.michalbialecki.com/2018/04/25/accept-xml-request-in-asp-net-mvc-controller/

Comments

1

with core 2, you need to specifically add the options to the mvc serverice to enable xml input/output:

    public void ConfigureServices(IServiceCollection services)
    {
        services.AddMvc(options =>
        {
            options.InputFormatters.Add(new XmlSerializerInputFormatter());
            options.OutputFormatters.Add(new XmlSerializerOutputFormatter());
        });
    }

and then change the Accept header to:

application/xml or application/json

Comments

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.