Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.NET Core 1.0 Web API doesn't return XML

How can I have my vnext API to return XML and JSON ?

I thought that using content-type with application/xml would work as it was before. Note that I tryed with Accept : application/xml too.

But it seems not.

EDIT :

this is my project.json file :

{
  "webroot": "wwwroot",
  "version": "1.0.0-*",

  "dependencies": {
    "Microsoft.AspNet.Server.IIS": "1.0.0-beta4",
    "Microsoft.AspNet.Server.WebListener": "1.0.0-beta4",
    "Microsoft.AspNet.Mvc": "6.0.0-beta4",
    "Microsoft.AspNet.Mvc.Xml": "6.0.0-beta4"
  },

  "commands": {
      "web": "Microsoft.AspNet.Hosting --server     Microsoft.AspNet.Server.WebListener --server.urls http://localhost:5000"
  },

  "frameworks": {
    "dnx451": { },
    "dnxcore50": { }
  },

  "publishExclude": [
    "node_modules",
    "bower_components",
    "**.xproj",
    "**.user",
    "**.vspscc"
  ],
  "exclude": [
    "wwwroot",
    "node_modules",
    "bower_components"
  ]
}

this is my startup.cs :

public class Startup
{
    // For more information on how to configure your application, visit http://go.microsoft.com/fwlink/?LinkID=398940
    public void ConfigureServices(IServiceCollection services)
    {
        services.ConfigureMvc(options =>
        {
            //options.AddXmlDataContractSerializerFormatter();

            options.InputFormatters.Add(new XmlSerializerInputFormatter());
            options.OutputFormatters.Add(new XmlSerializerOutputFormatter());
        });
        services.AddMvc();
    }

    public void Configure(IApplicationBuilder app)
    {
        app.UseMvc();
    }
}
like image 387
Tim Avatar asked Jun 03 '15 11:06

Tim


People also ask

Can ASP Net web API return XML?

Since the action method returns a complex object, the result can easily be switched to XML simply by changing the Accept header value. 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.

Does web API support XML?

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

By default Xml formatters are not included as part of the Microsoft.AspNet.Mvc package. You need to reference another package called Microsoft.AspNet.Mvc.Xml for this.

Example on how you can add the formatters:

public IServiceProvider ConfigureServices(IServiceCollection services)
{
    services.AddMvc();

    services.ConfigureMvc(options =>
    {
        // This adds both Input and Output formatters based on DataContractSerializer
        options.AddXmlDataContractSerializerFormatter();

        // To add XmlSerializer based Input and Output formatters.
        options.InputFormatters.Add(new XmlSerializerInputFormatter());
        options.OutputFormatters.Add(new XmlSerializerOutputFormatter());
    });
like image 151
Kiran Avatar answered Nov 03 '22 10:11

Kiran


Updated details following release of .Net Core 1.0.0

startup.cs

public void ConfigureServices(IServiceCollection services)
{
    // Add framework services.
    services.AddMvc(config =>
    {
        // Add XML Content Negotiation
        config.RespectBrowserAcceptHeader = true;
        config.InputFormatters.Add(new XmlSerializerInputFormatter());
        config.OutputFormatters.Add(new XmlSerializerOutputFormatter());
    });

project.json

"dependencies": {
    "Microsoft.AspNetCore.Mvc": "1.0.0",
    "Microsoft.AspNetCore.Mvc.Formatters.Xml": "1.0.0",

For more help see Shawn Wildermuths blog post on the subject: Content Negotiation in ASP.NET Core

like image 12
hsop Avatar answered Nov 03 '22 11:11

hsop