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?