[英]Problem sending model to HttpPost method in a .net core 3.1 web api application
I'm trying to hit my HttpPost method in a .net core 3.1 application.我正在尝试在 .net 核心 3.1 应用程序中使用我的 HttpPost 方法。 When i try to hit it, the model object is created but none of the properties are set.
当我尝试点击它时,会创建 model object 但没有设置任何属性。 The behaviour is same when i try to invoke using Swagger as well as postman.
当我尝试使用 Swagger 和 postman 调用时,行为是相同的。 I tried to add a model as simple as:
我尝试添加一个 model,如下所示:
public class Student{
public string Name{get; set;}
}
[HttpPost]
[Route("TestMethod")]
public void TestMethod(Student stu) {}
public void TestMethod(string name) {}
The strange behaviour is, if i pass it as query string, it works, but somehow it doesn't work with json (using postman).奇怪的行为是,如果我将它作为查询字符串传递,它可以工作,但不知何故它不适用于 json(使用邮递员)。 Also, if i create a demo app, it works perfectly fine, but not with my existing application
此外,如果我创建一个演示应用程序,它可以正常工作,但不适用于我现有的应用程序
i used FromBody attribute as well.我也使用了 FromBody 属性。
Can someone help me with this?有人可以帮我弄这个吗?
you have to use this action你必须使用这个动作
[HttpPost]
[Route("TestMethod")]
public IActionResult TestMethod([FromBody]Student stu) { return Ok(stu.Name)}
For a Post acton using Postman you have to select Body, raw, JSON in Postman menus.对于使用 Postman 的 Post 操作,您必须 select 主体,原始,JSON 在 Z6F061E1CAD11C1D7A287401D148A23BBD7A2F8Z 菜单中。 Your json should be
你的 json 应该是
{ "Name":"Doe" }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.