看起来像那样
XmlDataContractSerializer 输出 Formatter()有效,因为在其他情况下我可以输出XML,但是XmlDataContractSerializer 输入 Formatter())不起作用< / strong>;
我尝试使用
setupAction.AddXmlDataContractSerializerFormatters()
或将参数传递给
setupAction.InputFormatters.Add(新的XmlDataContractSerializerInputFormatter( setupAction )
但无济于事
services.AddMvc(setupAction =>
{
setupAction.ReturnHttpNotAcceptable = true;
setupAction.OutputFormatters.Add(new XmlDataContractSerializerOutputFormatter());
setupAction.InputFormatters.Add(new XmlDataContractSerializerInputFormatter());
});
[HttpPost]
public ActionResult CreateAuthor([FromBody] AuthorForCreationDto author)
{
if (author is null)
{
return BadRequest();
}
...
}
public class AuthorForCreationDto
{
public string FirstName { get; set; }
public string LastName { get; set; }
public DateTimeOffset DateOfBirth { get; set; }
public string Genre { get; set; }
public ICollection<BookForCreationDto> Books { get; set; }
= new List<BookForCreationDto>();
}
XML
<AuthorForCreationDto xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://schemas.datacontract.org/2004/07/Library.API.Models">
<DateOfBirth xmlns:d3p1="http://schemas.datacontract.org/2004/07/System">
<d3p1:DateTime>1948-03-03T00:00:00Z</d3p1:DateTime>
<d3p1:OffsetMinutes>0</d3p1:OffsetMinutes>
</DateOfBirth>
<FirstName>James</FirstName>
<Genre>Thriller</Genre>
<LastName>Ellroy</LastName>
</AuthorForCreationDto>
在邮递员中,我设置了Content-Type application / xml和Accept application / json