【问题标题】:Object value received incorrectly while passing complex object to web API将复杂对象传递给 Web API 时对象值接收不正确
【发布时间】:2021-09-13 10:44:40
【问题描述】:

我在这里使用 ASP .Net Core Web API 和 Angular。 'code' 和 'pageInfo' 值都在 console正确显示,但是当传递给 API 时,String 值被传递正确object值显示错误值(0和null)。

API-

[HttpPost("get/{code}/{pageInfo}")]
        public IActionResult Get(string code, Model pageInfo)
        {
            var data = _Service.GetData(code, pageInfo);
            if (data != null)
            {
                ---
            }
            else
            {
                ---
            }
        }

服务-

getAllData(code:any,pageInfo:any):Observable<any>{    
    console.log(code, pageInfo);
    const url = `/get/${code}/${pageInfo}`;    
    return this.http.post<any>(url, code, pageInfo);
  }

谁能帮我找出我在这里犯了什么错误?

【问题讨论】:

    标签: angular asp.net-core http-post webapi


    【解决方案1】:

    我已经通过一些修改解决了我的问题-

    • 更改参数顺序
    • 使用 [FromBody]

    API-

    [HttpPost("get/{code}/{pageInfo}")]
    public IActionResult Get([FromBody] Model pageInfo, string code)
    {
        var data = _Service.GetData(pageInfo, code);
        if (data != null)
          {---}
        else
          {---}
    }
    

    服务-

    getAllData(pageInfo:any, code:any):Observable<any>{    
        const url = `/get/${code}`;    // have passed only the string value via URL
        return this.http.post<any>(url, pageInfo, code);
      }
    

    【讨论】:

      猜你喜欢
      • 2016-06-12
      • 2023-03-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-01-06
      • 2014-10-28
      相关资源
      最近更新 更多