将多个对象从 Angular 8 发送到 .Net Core Web Api - 控制器端为空
Sending Multiple Objects from Angular 8 to .Net Core Web Api - Null at Controller End
我有以下方法将多个对象发送到 .Net Core Web API
this.http.post('api/InternalReport/create', [
this.internalreport,
this.file]
, { observe: 'response', responseType: 'text' }).subscribe(data => {
if (data.status == 200) { console.log("success");}
}, err => { console.error('Observer got an error:');});
API 控制器
[HttpPost("create")]
public ActionResult<InternalReport> Create([FromBody]InternalReport report,[FromBody]File file)
{
try
{
_internalreportService.Create(report);
_fileservice.Update(file.sha1,file);
return Ok("success");
}
catch (Exception e)
{
return NotFound();
}
}
但是这两个对象在控制器端都是空的。
使用JsonType,只能传递一个对象,如果要传递多个对象,需要新建一个包含多个对象的对象。
这是一个有效的演示:
角度数据:
public class AngularData
{
public Student student { get; set; }
public string a { get; set; }
}
学生:
public class Student
{
public string Gender { get; set; }
}
发送数据:
SendData() {
var student={'Gender':'male'};
var angulardata={student,'a':'aaa'}
const headers = new HttpHeaders().set('Content-Type','application/json');
this.http.post('https://localhost:44363/create',JSON.stringify(angulardata),{headers:headers})
.subscribe(data => {
});
控制器:
[HttpPost("create")]
public IActionResult Create([FromBody]AngularData angularData)
{
return Ok();
}
结果:
我有以下方法将多个对象发送到 .Net Core Web API
this.http.post('api/InternalReport/create', [
this.internalreport,
this.file]
, { observe: 'response', responseType: 'text' }).subscribe(data => {
if (data.status == 200) { console.log("success");}
}, err => { console.error('Observer got an error:');});
API 控制器
[HttpPost("create")]
public ActionResult<InternalReport> Create([FromBody]InternalReport report,[FromBody]File file)
{
try
{
_internalreportService.Create(report);
_fileservice.Update(file.sha1,file);
return Ok("success");
}
catch (Exception e)
{
return NotFound();
}
}
但是这两个对象在控制器端都是空的。
使用JsonType,只能传递一个对象,如果要传递多个对象,需要新建一个包含多个对象的对象。 这是一个有效的演示:
角度数据:
public class AngularData
{
public Student student { get; set; }
public string a { get; set; }
}
学生:
public class Student
{
public string Gender { get; set; }
}
发送数据:
SendData() {
var student={'Gender':'male'};
var angulardata={student,'a':'aaa'}
const headers = new HttpHeaders().set('Content-Type','application/json');
this.http.post('https://localhost:44363/create',JSON.stringify(angulardata),{headers:headers})
.subscribe(data => {
});
控制器:
[HttpPost("create")]
public IActionResult Create([FromBody]AngularData angularData)
{
return Ok();
}
结果: