首页 > 解决方案 > Angular httpclient.post 向 ASP.Net Core Action 发送 JSON Obj,在后端接收到的数据为 null

问题描述

我有一个项目是 ASP.Net Core 和 Angular。我正在尝试做一个非常简单的操作,从 Angular 将 JSON 数据格式发布到后端 ASP.Net Core。该流程似乎有效,它在后端调用了正确的操作,但是数据传递为空。感谢我能得到的任何帮助。谢谢

在前端,我有一个简单的对象来保存数据,在构造函数中我将其转换为 JSON。我在 http.post 调用中验证了数据部分包含 JSON 信息。所以在下面的调用中 jsonMessageFormat 有正确的信息。

   this._http.post(this._baseUrl + 'fabAutomation/PostTest', 
    this.jsonMessageFormat, this.config).subscribe(result => {
    this._returnedResult = result;
     }, error => console.error(error));

在后端,我尝试将 PostTest Action 设置为同时接受: 1. 字符串 2. [FromBody] 格式从 JSON 转换为模型

两者都没有,选项有效。并且在后端操作中接收到的数据为空。

在前端:

   fabAutomationJsonMessage: any = [{ "maskID": "AAAA", "recipeName": 
   "XXXX", "JobTypeCduOrReg": "reg", "cduRevNumber": "0", "regRevNumber": 
  "0", "recipeState": null, "reticleAvailability": "eee", "dateTime": 
  "ddd" }];

   jsonMessageFormat: JSON;


 config: any = new HttpHeaders().set('Content-Type', 'application/json')
.set('Accept', 'application/json')




     constructor(http: HttpClient, @Inject('BASE_URL') baseUrl: string)
        {
       this.jsonMessageFormat = <JSON>this.fabAutomationJsonMessage;
        this._http = http;
       this._baseUrl = baseUrl;
        }


forSubmitOnClick() {

this._http.post(this._baseUrl + 'fabAutomation/PostTest', 
this.jsonMessageFormat, this.config).subscribe(result => {
  this._returnedResult = result;
}, error => console.error(error));

//在后端:

//在Controller处,定义如下Action,fabAutomationMessageVMOBJ始终为null

        [HttpPost("[action]")]
        public IActionResult PostTest([FromBody] fabAutomationMessageVM 
        fabAutomationJsonMessageVMOBJ)
          {


        //removed implementation for simplicity


            //for response simply return the same information
            return Json(fabAutomationJsonMessageVMOBJ);


          }

由于整个流程都在工作,并且 http.post 包含 JSON 格式的数据(非空),我希望这些数据将作为非空传递到后端。

标签: angularasp.net-core-mvchttp-post

解决方案


首先,我要感谢大家的帮助,特别是对一直陪在我身边的邹星。我发现问题出在viewModel上。尽管我将类声明为 public,但我发现我还必须将每个成员声明为 public。


推荐阅读