Asp.net core mvc [FromBody]。Post控制器中的参数值始终为空
我不熟悉.net web应用程序。我的用例是使用XML作为主体进行post调用。我试图通过邮递员进行呼叫,但在我的控制器中收到的参数值始终为空。以下是我所拥有的东西: 他是我的身体:Asp.net core mvc [FromBody]。Post控制器中的参数值始终为空,asp.net-core-mvc,asp.net-mvc-controller,Asp.net Core Mvc,Asp.net Mvc Controller,我不熟悉.net web应用程序。我的用例是使用XML作为主体进行post调用。我试图通过邮递员进行呼叫,但在我的控制器中收到的参数值始终为空。以下是我所拥有的东西: 他是我的身体: <?xml version="1.0" encoding="utf-8"?> <document> <id>123456</id> <content>This is document that
<?xml version="1.0" encoding="utf-8"?>
<document>
<id>123456</id>
<content>This is document that I posted...</content>
<author>Michał Białecki</author>
<links>
<link>2345</link>
<link>5678</link>
</links>
</document>
除此之外,我还在Startup.cs中添加了这个
services.AddMvc().AddXmlDataContractSerializerFormatters()代码>
最后这是我的控制器:
[Route("api/[controller]")]
public class UploadFileController : ControllerBase
{
[HttpPost]
[Route("upload")]
public void RegisterDocument([FromBody] Document dto)
{
Console.WriteLine("Inside the controller");
}
}
这是我在《邮差》中的称呼:
在调试模式下,我注意到的另一件事是,我也看到了这些错误:
有人能帮忙吗?我尝试过各种解决方案,但都没能奏效。提前感谢。您需要添加AddXmlSerializerFormatters()
:
您的xml应该如下所示(删除
):
结果:
你好,谢谢你的回答。它正在工作。添加“.AddXmlSerializerFormatters()”我不知道我怎么会错过它。看起来很琐碎。
[Route("api/[controller]")]
public class UploadFileController : ControllerBase
{
[HttpPost]
[Route("upload")]
public void RegisterDocument([FromBody] Document dto)
{
Console.WriteLine("Inside the controller");
}
}
services.AddMvc()
.AddXmlSerializerFormatters()
.AddXmlDataContractSerializerFormatters();
<document>
<id>123456</id>
<content>This is document that I posted...</content>
<author>Michał Białecki</author>
<links>
<link>2345</link>
<link>5678</link>
</links>
</document>
[HttpPost]
public void Post([FromBody] ABC dto)
{
}