question

KenMartos-3440 avatar image
0 Votes"
KenMartos-3440 asked KenMartos-3440 edited

Why my API is returning JSON format on Header "accept: application/xml"?

I'm building an API with OData integration returning response JSON and XML format, but why is my API returning JSON format instead of XML format on the header accept: application/xml

This is my Program.cs with Odata and XML formatter.


 using Microsoft.AspNetCore.Mvc.Formatters;
 using Microsoft.AspNetCore.OData;
    
 var builder = WebApplication.CreateBuilder(args);
    
 ...
    
 builder.Services.AddControllers()
     .AddOData(options => options.Select().Filter().OrderBy().Expand().OrderBy().Count());
    
 builder.Services.AddMvc(options =>
 {
     options.RespectBrowserAcceptHeader = true;
     options.ReturnHttpNotAcceptable = true;
     options.OutputFormatters.Add(new XmlSerializerOutputFormatter());
 });


And this is the controller I've been testing on.

 using Microsoft.AspNetCore.Mvc;
 using Microsoft.AspNetCore.OData.Formatter;
 using Microsoft.AspNetCore.OData.Query;
 using Microsoft.AspNetCore.OData.Routing.Controllers;
    
 namespace OData.Controllers
 {
     [Produces("application/json", "application/xml")]
     [ApiController]
     [Route("[controller]")]
     [FormatFilter]
     public class WeatherForecastController : ControllerBase
     {
         private static readonly string[] Summaries = new[]
         {
         "Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching"
         };
    
         private readonly ILogger<WeatherForecastController> _logger;
    
         public WeatherForecastController(ILogger<WeatherForecastController> logger)
         {
             _logger = logger;
         }
    
         [HttpGet(Name = "GetWeatherForecast")]
         [EnableQuery]
         [FormatFilter]
         public IActionResult Get()
         {
             return Ok(Enumerable.Range(1, 5).Select(index => new WeatherForecast
             {
                 Date = DateTime.Now.AddDays(index),
                 TemperatureC = Random.Shared.Next(-20, 55),
                 Summary = Summaries[Random.Shared.Next(Summaries.Length)]
             })
             .ToArray());
         }
     }
 }

what seems to be the problem?








dotnet-aspnet-core-generaldotnet-aspnet-core-mvcdotnet-aspnet-core-webapi
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

Bruce-SqlWork avatar image
0 Votes"
Bruce-SqlWork answered KenMartos-3440 edited

Xml is not really supported. See this issue thread.

https://github.com/OData/WebApi/issues/2330

· 3
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Hi @Bruce-SqlWork, actually, the only method that is not working on getting the XML format is the $select and $expand method, other method like $filter, $top and $orderby etc... is working fine.






0 Votes 0 ·

Hi @KenMartos-3440,

actually, the only method that is not working on getting the XML format is the $select and $expand method, other method like $filter, $top and $orderby etc... is working fine.

It seems that this issue relates the OData, you'd better to create OData GitHub issues or contact the OData Team.

1 Vote 1 ·

Hi @ZhiLv-MSFT, apparently when I contact them the mail cant go through I always received a reply mail like this 212623-image.png


0 Votes 0 ·
image.png (38.2 KiB)
image.png (38.5 KiB)