简体   繁体   English

将参数传递给控制器​​的 HttpPost 方法

[英]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.

相关问题 如何将这两个参数传递给控制器​​的动作方法? - How to pass these 2 parameters to controller action method? 如何将错误消息从 Web API 控制器的 HttpPost 异步方法返回任务传递给客户端<Type> ? - How to pass error message to client from Web API controller's HttpPost async method returning Task<Type>? 在简单情况下,HttpPost将null传递给Controller方法 - HttpPost in a simple scenario passes null to a Controller method Web API 控制器中的多个 HttpPost 方法 - Multiple HttpPost method in Web API controller 将复杂的ViewModel传递给MVC 3中的HttpPost方法 - Pass Complex ViewModel to HttpPost Method in MVC 3 EDIT NEW 使用 HttpPost 将数据从控制器传递到视图 - EDIT NEW Pass data from the controller to the view using HttpPost 从HttpGet操作方法获取数据并将其传递给HttpPost方法 - Get Data from HttpGet Action Method and pass them to HttpPost Method 如何将复合数组从Javascript传递到WEB API控制器HttpPOST? - How to pass a composite array from Javascript to WEB API controller HttpPOST? 使用HttpPost将对象列表从View传递到控制器 - Pass list of object from View to controller using HttpPost 向ASP.NET MVC控制器中的操作方法发出HttpPost请求 - Make HttpPost request to an action method in an ASP.NET MVC controller
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM