[英]Pass parameters to HttpPost method of controller
There something very basic i must be missing which i cant understand what it is.我必须缺少一些非常基本的东西,我无法理解它是什么。 I have a method at the controller
我在控制器上有一个方法
[Route("SomeMethod")]
[HttpPost]
public IActionResult SomeMethod([FromBody]int interval)
{
...
}
And im seing with postman post request {"interval": 2000 }
, The interval at the controller is not initialized.我看到 postman post request
{"interval": 2000 }
,控制器的间隔没有初始化。 If i change the method paramater to be object i get ValueKind = Object : "{"interval": 2000 }"
如果我将方法参数更改为对象,我会得到
ValueKind = Object : "{"interval": 2000 }"
I also tried the parameter to be string and then the body i sent is {"interval": "2000" }
, And at the controller i get null我还尝试将参数设为字符串,然后我发送的正文是
{"interval": "2000" }
,在控制器上我得到 null
Based on this question i tried:基于这个问题,我试过:
[Route("SomeMethod")]
[HttpPost]
public IActionResult SomeMethod([FromBody]JsonElement interval)
{
...
}
And got ValueKind = Undefined : ""
At the contoroller, And also:并得到
ValueKind = Undefined : ""
在控制器上,还有:
services.AddControllers()
.AddNewtonsoftJson();
When the method expects an int and got 0当方法需要一个 int 并且得到 0 时
According to documentation you have to pass it in request body as it is, just a value, without property name.根据文档,您必须按原样将它传递到请求正文中,只是一个值,没有属性名称。 But its imposible to bind more then one parameter.
但是不可能绑定多个参数。 Anyway, I would reccomend to use proper object to bind body parameters.
无论如何,我建议使用适当的对象来绑定主体参数。
[Route("SomeMethod")]
[HttpPost]
public IActionResult SomeMethod([FromBody]Request request)
{
return Ok();
}
public class Request
{
public int Interval { get; set; }
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.