【问题标题】:controller gets request parameter null in http post mthod控制器在http post方法中获取请求参数null
【发布时间】:2020-05-20 13:36:57
【问题描述】:

我通过 Postman 测试我的帖子并获得数据,当我发布以下网址时(我添加了硬编码的 ip):

http://localhost:59628/api/AcessInfo/?id=192.168.10.801

但是当我尝试同样的事情时,我在 .netcore 中得到 id=null

url: http://localhost:59628/api/AcessInfo/
 this.http.post(this.url,{params:{id:x} });

我的控制器:

       public IActionResult turbineDvic(string id)
        {


            var x = _acess.Devices(id);



            return Ok(x);

        }

我试过了:

([FromBody]string id)

但这给出了我的错误 405!我从未见过,有什么想法吗?似乎我无法将参数添加到我的网址

【问题讨论】:

  • 在邮递员中您正在尝试查询参数?是 GET 调用还是 POST 调用?
  • @SelakaN 发帖
  • 所以我没有看到您在 Angular 应用程序中提供的 url 中的 IP 地址部分?例如:它只是这个http://localhost:59628/api/AcessInfo/ 你错过了完整的网址http://localhost:59628/api/AcessInfo/?id=192.168.130.101 检查是否是问题,让我知道我会发布答案
  • @SelakaN 在我的控制器中我通过角度得到 id=null 这就是问题
  • 您确定这是一个 Options 请求到达您的控制器吗?

标签: angular asp.net-core


【解决方案1】:

HttpClient 的 post 方法需要一个 body 作为第二个参数。尝试传递一个空的身体

this.http.post(this.url, {}, { params:{id:x} }

【讨论】:

  • 哇!工作,你能告诉我那个身体通常是什么吗?为什么我们需要它
  • 正文是您通常使用 http post 请求发送的数据。 stackoverflow.com/q/16339198/1160794
猜你喜欢
  • 1970-01-01
  • 2016-06-08
  • 2014-11-26
  • 2012-11-06
  • 1970-01-01
  • 1970-01-01
  • 2014-07-02
  • 1970-01-01
相关资源
最近更新 更多