.NETCore3-api-XML和JSON格式在URL请求时输出(.XML、.JSON)

.NETCore3-api-XML和JSON格式在URL请求时输出(.XML、.JSON),.net,api,format,asp.net-core-3.0,.net,Api,Format,Asp.net Core 3.0,我试图弄清楚如何设置内容协商,以便应用程序能够根据请求同时使用xml或json 我尝试了很多你可以在互联网上找到的东西,但都没有用。(通过我的注释代码,您可以看到这一点)。我试过把所有的选项都放进去。AddMvc,把所有的选项都拿出来,等等。我只是不知道下一步该怎么做 以下是ConfigureServices调用的初始部分 public void ConfigureServices(IServiceCollection services) { services.AddCo

我试图弄清楚如何设置内容协商,以便应用程序能够根据请求同时使用xml或json

我尝试了很多你可以在互联网上找到的东西,但都没有用。(通过我的注释代码,您可以看到这一点)。我试过把所有的选项都放进去。AddMvc,把所有的选项都拿出来,等等。我只是不知道下一步该怎么做

以下是ConfigureServices调用的初始部分

public void ConfigureServices(IServiceCollection services)
    {
        services.AddControllersWithViews();
        //services.AddControllers().AddXmlSerializerFormatters();
        services.AddControllers().AddNewtonsoftJson();
        services.AddControllers(options =>
        {
            options.ReturnHttpNotAcceptable = true;
            options.OutputFormatters.RemoveType<HttpNoContentOutputFormatter>();
            options.OutputFormatters.Add(new XmlSerializerOutputFormatter());

            options.FormatterMappings.SetMediaTypeMappingForFormat(
                                         "xml", "application/xml");
        });
        //services.AddControllers(options =>
        //{
        //    options.FormatterMappings.SetMediaTypeMappingForFormat
        //        ("xml", MediaTypeHeaderValue.Parse("application/xml"));
        //}).AddXmlSerializerFormatters().AddXmlDataContractSerializerFormatters();
public void配置服务(IServiceCollection服务)
{
services.AddControllersWithViews();
//services.AddControllers().AddXmlSerializerFormatters();
services.AddControllers().AddNewtonsoftJson();
services.AddControllers(选项=>
{
options.ReturnHttpNotAcceptable=true;
options.OutputFormatters.RemoveType();
options.OutputFormatters.Add(新的XmlSerializerOutputFormatter());
options.FormatterMappings.SetMediaTypeMappingForFormat(
“xml”、“应用程序/xml”);
});
//services.AddControllers(选项=>
//{
//options.FormatterMappings.SetMediaTypeMappingForFormat
//(“xml”,MediaTypeHeaderValue.Parse(“application/xml”);
//}).AddXmlSerializerFormatters().AddXmlDataContractSerializerFormatters();
下面是我在控制器中设置此项的尝试

[Route("api/[controller]")]
[ApiController]
[FormatFilter]
public class PulpTestsController : ControllerBase
{
    private readonly DatabaseContext _context;

    public PulpTestsController(DatabaseContext context)
    {
            _context = context;
    }

    // GET: api/PulpTests
    [HttpGet()]
    [HttpGet(".{format?}")]
    //[HttpGet(".{format?}"),FormatFilter]
    public async Task<ActionResult<IEnumerable<PulpTests>>> GetPulpTests()
    {
        return await _context.PulpTests.Include(b=>b.System).ToListAsync();
    }
[路由(“api/[控制器]”)]
[ApiController]
[格式过滤器]
公共类纸浆测试控制器:ControllerBase
{
私有只读数据库上下文\u上下文;
公共测试控制器(DatabaseContext上下文)
{
_上下文=上下文;
}
//获取:api/纸浆测试
[HttpGet()]
[HttpGet(“.{format?}”)]
//[HttpGet(“.{format?}”),FormatFilter]
公共异步任务getTests()
{
return wait_context.pulltests.Include(b=>b.System.tolistSync();
}

好吧-如果有人有我的相同问题,我对api进行了更改,以重新定义路由并接受格式:

[Route("api/[controller]")]
[ApiController]    
[FormatFilter]
public class PulpTestsController : ControllerBase
{
    private readonly DatabaseContext _context;

    public PulpTestsController(DatabaseContext context)
    {
        _context = context;
    }

    // GET: api/PulpTests
    [Route("/api/[controller].{format}")] // <- Right here
    [HttpGet]
    public async Task<ActionResult<IEnumerable<PulpTests>>> GetPulpTests()
    {
        return await _context.PulpTests.Include(b=>b.SystemEntity).ToListAsync();
    }
[路由(“api/[控制器]”)]
[ApiController]
[格式过滤器]
公共类纸浆测试控制器:ControllerBase
{
私有只读数据库上下文\u上下文;
公共测试控制器(DatabaseContext上下文)
{
_上下文=上下文;
}
//获取:api/纸浆测试
[Route(“/api/[controller].{format}”)]///b.SystemEntity.ToListAsync();
}
我还移除了startup.cs上的一堆锅炉板

    public void ConfigureServices(IServiceCollection services)
    {
        services.AddControllersWithViews();
        //services.AddControllers().AddXmlSerializerFormatters();
        services.AddControllers().AddNewtonsoftJson();
        services.AddControllers(options =>
        {
            options.RespectBrowserAcceptHeader = true;
            options.ReturnHttpNotAcceptable = true;                
            //options.OutputFormatters.RemoveType<HttpNoContentOutputFormatter>();
            //options.InputFormatters.Add(new XmlSerializerInputFormatter(options));
            //options.OutputFormatters.Add(new XmlSerializerOutputFormatter());
            //options.FormatterMappings.SetMediaTypeMappingForFormat(
            //                             "xml", "application/xml");
        }).AddXmlSerializerFormatters();
public void配置服务(IServiceCollection服务)
{
services.AddControllersWithViews();
//services.AddControllers().AddXmlSerializerFormatters();
services.AddControllers().AddNewtonsoftJson();
services.AddControllers(选项=>
{
options.reserverBrowserAcceptHeader=true;
options.ReturnHttpNotAcceptable=true;
//options.OutputFormatters.RemoveType();
//添加(新的XmlSerializerInputFormatter(选项));
//options.OutputFormatters.Add(新的XmlSerializerOutputFormatter());
//options.FormatterMappings.SetMediaTypeMappingForFormat(
//“xml”、“应用程序/xml”);
}).AddXmlSerializerFormatters();