angular - 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 格式的数据(非空),我希望这些数据将作为非空传递到后端。
解决方案
首先,我要感谢大家的帮助,特别是对一直陪在我身边的邹星。我发现问题出在viewModel上。尽管我将类声明为 public,但我发现我还必须将每个成员声明为 public。
推荐阅读
- spring-boot - 如何从spring控制器中的请求中获取所有表单数据条目
- c - 如何通过标准输入停止输入溢出
- node.js - 将类组件转换为函数组件
- c# - 将 System.Diagnostics.Stopwatch 从文本文件排序到前 10 名
- php - 在 laravel 中提取分钟到小时
- java - Java 流 sorted() 行为取决于下一个 takeWhile() 调用
- reactjs - 来自 AWS Amplify 上托管的反应应用程序的受保护路由的访问被拒绝错误
- c - 写入文件时如何删除空格和换行符?
- android - 阅读 XSSF 工作簿 (.xlsx) 会使 Android 应用程序崩溃
- c - 有没有办法在 Linux 中拦截文件处理请求?