[英]Angular POST request received in NET Core Api as Null
Im POSTing some data via Angular 6, but my Core API keeps returning nulls:我通过 Angular 6 发布一些数据,但我的核心 API 不断返回空值:
Request:要求:
{"id":0,"name":"test","weight":2,"frequency":2,"activityTypeModelId":3}
Response:回复:
{id: 0, name: null, weight: 0, frequency: 0, activityTypeModelId: 0}
Controller:控制器:
[HttpPost("[action]")]
public IActionResult Add([FromForm]Model model)
{
return new JsonResult(model);
}
Angular, using HttpClient: Angular,使用 HttpClient:
add(Model: model) {
return this.http.post(this.addUrl, model);
}
API Model:接口模型:
public class Model
{
[Key]
public int Id { get; set; }
[Required]
public string Name { get; set; }
[Required]
public int Weight { get; set; }
public int Frequency { get; set; }
public int ActivityTypeModelId { get; set; }
}
TS Model: TS型号:
export class Model{
id?: number;
name?: string;
weight?: number;
frequency?: number;
activityTypeModelId?: number;
}
Everything works fine when I'm using Postman.当我使用 Postman 时,一切正常。 I already tried with [FromBody].
我已经尝试过 [FromBody]。 Where is the problem?
问题出在哪儿?
I dont know why, but this fixed my issue: 我不知道为什么,但这解决了我的问题:
I created a header: 我创建了一个标题:
const header = new HttpHeaders()
.set('Content-type', 'application/json');
Changed the POST function by adding a header and JSON.Stringyfy the object: 通过添加标头和JSON.Stringyfy更改了POST函数:
add(model: Model): Observable<Model> {
const body = JSON.stringify(c);
return this.http.post<Model>(this.addUrl, body, { headers: header} );
}
Changed [FromForm]
to [FromBody]
. 将
[FromForm]
更改为[FromBody]
。
Adding JSON.stringify(model)
in the parameters of the http.post
was not working. 无法在
http.post
的参数中添加JSON.stringify(model)
。
JSON that is working with the CORE Api: 使用CORE Api的JSON:
{"name":"test","weight":2,"activityTypeModelId":15}
JSON that is not working with the CORE Api: 无法与CORE Api配合使用的JSON:
{name:"test",weight:2,activityTypeModelId:15}
Without the header I encountered a 415 error from the API. 没有标题,我从API遇到415错误。
尝试
return this.http.post(this.addUrl, JSON.stringify(model) );
I think that, in .NET Core 2.1 is (see https://docs.microsoft.com/en-us/aspnet/core/web-api/action-return-types?view=aspnetcore-2.1 ) 我认为,在.NET Core 2.1中是(请参阅https://docs.microsoft.com/zh-cn/aspnet/core/web-api/action-return-types?view=aspnetcore-2.1 )
HttpPost("[action]")]
//see that I put [FromBody]
public IActionResult Add([FromBody]Model model)
{
//OK is one of several IActionResult
return OK(model);
}
I had this issue and the problem was that I was trying to bind to an interface:我遇到了这个问题,问题是我试图绑定到一个接口:
[HttpPost("[action]")]
[ProducesResponseType(typeof(bool), StatusCodes.Status200OK]
public bool SaveResponse([FromBody]ISaveRequestViewModel request) =>
Service.SaveResponse(request.Responses);
I fixed it by changing it to a class:我通过将其更改为一个类来修复它:
[HttpPost("[action]")]
[ProducesResponseType(typeof(bool), StatusCodes.Status200OK]
public bool SaveResponse([FromBody]SaveRequestViewModel request) =>
Service.SaveResponse(request.Responses);
This model also had to use classes instead of interfaces:该模型还必须使用类而不是接口:
public class SaveRequestViewModel
{
public List<IQuestionResponseViewModel> Responses { get; set; }
}
Became成为
public class SaveRequestViewModel
{
public List<QuestionResponseViewModel> Responses { get; set; }
}
I guess the model binder just doesn't work with interfaces.我猜模型绑定器不适用于接口。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.