【发布时间】:2020-07-11 07:49:06
【问题描述】:
我正在尝试在 .NET Core 3.1 上创建一个 WebAPI 控制器,它支持 JSON 和 XML 作为请求/响应内容类型。
控制器在接收带有“application/json”的 JSON 时可以正常工作,但是当它接收带有“application/xml”的 XML 时,方法参数是使用默认值创建的,而不是在请求正文中发布的值。
示例项目 - https://github.com/rincew1nd/ASPNetCore_XMLMethods
启动中的附加 XML 序列化程序:
services.AddControllers().AddXmlSerializerFormatters();
带有方法和测试模型的控制器:
[ApiController]
[Route("[controller]")]
public class TestController : ControllerBase
{
[HttpPost, Route("v1")]
[Consumes("application/json", "application/xml")]
[Produces("application/json", "application/xml")]
public TestRequest Test([FromBody] TestRequest data)
{
return data;
}
}
[DataContract]
public class TestRequest
{
[DataMember]
public Guid TestGuid { get; set; }
[DataMember]
public string TestString { get; set; }
}
附:项目包含用于 API 测试目的的 Swagger。
【问题讨论】:
-
xml请求的示例数据需要
TestGuid而不是testGuid和TestString而不是testString
标签: asp.net-core asp.net-web-api xml-serialization asp.net-core-3.1