【问题标题】:Angular POST request received in NET Core Api as Null在 NET Core Api 中收到的 Angular POST 请求为 Null
【发布时间】:2018-08-30 18:31:24
【问题描述】:

我正在通过 Angular 6 发布一些数据,但我的核心 API 不断返回空值:

请求:

{"id":0,"name":"test","weight":2,"frequency":2,"activityTypeModelId":3}

回复:

{id: 0, name: null, weight: 0, frequency: 0, activityTypeModelId: 0}

控制器:

[HttpPost("[action]")]
public IActionResult Add([FromForm]Model model)
{
    return new JsonResult(model);
}

Angular,使用 HttpClient:

add(Model: model) {
     return this.http.post(this.addUrl, model);
}

API 模型:

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 型号:

 export class Model{
   id?: number;
   name?: string;
   weight?: number;
   frequency?: number;
   activityTypeModelId?: number;
 }

当我使用 Postman 时,一切正常。我已经尝试过 [FromBody]。问题出在哪里?

【问题讨论】:

  • 你的操作返回了一个名为newCircumstance的东西,那是什么?
  • 您在浏览器的开发者控制台的网络选项卡中看到了什么?那边能看到request body吗?
  • 当你闯入 C# 动作时是否填充了模型?当您查看浏览器开发工具时,提交的是什么?
  • @R.Richards ,粘贴后我忘记在此处更改该名称。我在编辑中更改了它。
  • @AmitChigadani 不,没有请求主体。

标签: c# angular asp.net-core


【解决方案1】:

我不知道为什么,但这解决了我的问题:

我创建了一个标题:

 const header = new HttpHeaders()
     .set('Content-type', 'application/json');

通过添加标头和 JSON.Stringyfy 对象来更改 POST 函数:

 add(model: Model): Observable<Model> {
     const body = JSON.stringify(c);
     return this.http.post<Model>(this.addUrl, body, { headers: header} );
   }

[FromForm] 更改为[FromBody]

http.post 的参数中添加JSON.stringify(model) 不起作用。

使用 CORE Api 的 JSON:

{"name":"test","weight":2,"activityTypeModelId":15}

不适用于 CORE Api 的 JSON:

{name:"test",weight:2,activityTypeModelId:15}

如果没有标头,我遇到了来自 API 的 415 错误。

【讨论】:

    【解决方案2】:

    试试

    return this.http.post(this.addUrl, JSON.stringify(model) );
    

    【讨论】:

      【解决方案3】:

      我认为,在 .NET Core 2.1 中是(参见 https://docs.microsoft.com/en-us/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);
      }
      

      【讨论】:

        【解决方案4】:

        我遇到了这个问题,问题是我试图绑定到一个接口:

        [HttpPost("[action]")]
        [ProducesResponseType(typeof(bool), StatusCodes.Status200OK]
        public bool SaveResponse([FromBody]ISaveRequestViewModel request) =>
            Service.SaveResponse(request.Responses);
        

        我通过将其更改为一个类来修复它:

        [HttpPost("[action]")]
        [ProducesResponseType(typeof(bool), StatusCodes.Status200OK]
        public bool SaveResponse([FromBody]SaveRequestViewModel request) =>
            Service.SaveResponse(request.Responses);
        

        这个模型还必须使用类而不是接口:

        public class SaveRequestViewModel
        {
            public List<IQuestionResponseViewModel> Responses { get; set; }
        }
        

        成为

        public class SaveRequestViewModel
        {
            public List<QuestionResponseViewModel> Responses { get; set; }
        }
        

        我猜模型绑定器不适用于接口。

        【讨论】:

          猜你喜欢
          • 2021-12-18
          • 2020-10-20
          • 2019-08-19
          • 2018-01-24
          • 1970-01-01
          • 1970-01-01
          • 2017-06-06
          • 2021-10-27
          • 2017-03-29
          相关资源
          最近更新 更多