.Net Core 3 - api - XML 和 JSON 格式根据 URL 请求输出 (.xml, .json)
.Net Core 3 - api - XML and JSON format outputs upon URL request (.xml, .json)
我正在尝试弄清楚如何设置内容协商,以便应用程序服务器根据请求提供 xml 或 json。
我已经尝试了多种您可以在互联网上找到的东西,包括 microsoft documentation 但无济于事。 (您会在我的注释代码中看到这一点)。我试过将所有选项放入 .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();
这是我在控制器中进行设置的尝试
[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 进行了更改以重新定义路线并接受格式:
[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();
}
我还删除了 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();
我正在尝试弄清楚如何设置内容协商,以便应用程序服务器根据请求提供 xml 或 json。
我已经尝试了多种您可以在互联网上找到的东西,包括 microsoft documentation 但无济于事。 (您会在我的注释代码中看到这一点)。我试过将所有选项放入 .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();
这是我在控制器中进行设置的尝试
[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 进行了更改以重新定义路线并接受格式:
[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();
}
我还删除了 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();