I have a project that is ASP.Net Core and Angular. I am trying to do a very simple operation, from Angular posting a JSON data format to the back-end ASP.Net Core. The flow seems to work, it calls the correct Action at the back-end, however the data pass is null. Appreciate any help I can get. Thanks
At the Front-End, I have a simple Object that holds the data and at constructor I cast it to JSON. I verified that at the http.post call the data part contain the JSON information. So at the call below the jsonMessageFormat has the correct information.
this._http.post(this._baseUrl + 'fabAutomation/PostTest',
this.jsonMessageFormat, this.config).subscribe(result => {
this._returnedResult = result;
}, error => console.error(error));
At the back end I tried to set the PostTest Action to accept both: 1. string 2. [FromBody] format to convert from JSON to the model
Neither, option worked. And at the Back-End Action the received data is null.
At the front-end:
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));
//At the BackEnd:
//At the Controller, define the following Action, the fabAutomationMessageVMOBJ is always null
[HttpPost("[action]")]
public IActionResult PostTest([FromBody] fabAutomationMessageVM
fabAutomationJsonMessageVMOBJ)
{
//removed implementation for simplicity
//for response simply return the same information
return Json(fabAutomationJsonMessageVMOBJ);
}
Since the whole flow is working, and the http.post contain the Data in a JSON format (not null), I expect that this data will pass to the back-end as none-null.
fabAutomationJsonMessagecan be passed as is. You don't really need to type cast it to<JSON>- SiddAjmera[FromBody]from your Action - Zze