【问题标题】:Angular6 POST request remains in pending statusAngular6 POST 请求仍处于待处理状态
【发布时间】:2019-11-09 15:14:47
【问题描述】:

您好,我正在尝试将来自 angular6 的请求发布到 asp.net core web api,但不幸的是,该请求从未在服务器上收到,并且始终处于“待处理”状态。

我在服务器上的模型:

public class MainVM
{
        public int ID { get; set; }
        public string Name { get; set; }
        public bool IsActive { get; set; }
        public int DepartmentID { get; set; }
        public IFormFile Logo { get; set; }
        public List<ChildVM> ListChilds { get; set; }

}

public class ChildVM
{
        public string Name { get; set; }
        public int SequenceNo { get; set; }
        public int NoOfPrices { get; set; }
        public IFormFile Image { get; set; }
}

和终点:

[HttpPost]
[Consumes("multipart/form-data")]
public void Post([FromForm]MainVM data)
{
}

我用来从 Angular 发布数据的 Angular6 服务:

const formData: FormData = new FormData();
    //formData.append('Logo', logo, logo.name);
    formData.append('Name', obj.Name);
    formData.append('ListChilds[0].Name', obj.ListChilds[0].Name);
    formData.append('ListChilds[0].SequenceNo', String(obj.ListChilds[0].SequenceNo));
    formData.append('ListChilds[0].NoOfPrices', String(obj.ListChilds[0].NoOfPrices));



    return this.http.post<MainVM>('http://localhost:60458/api/mycontroller/', formData).pipe(
      map((res) => { console.log('res'); return res; }),
      catchError(this.handleError('lpc', null))
    );

当我在 formData 中注释掉与 ListChilds 相关的代码时,请求在 web api 处收到,但请求未在带有 ListChilds 的 web api 处收到。

我想我错过了什么。

请指导。

谢谢。

【问题讨论】:

    标签: c# asp.net-web-api angular6 asp.net-core-2.2


    【解决方案1】:

    试试这个 -

     formData.append('ListChilds', JSON.stringify(obj.ListChilds));
    

    它应该将 obj.ListChilds 中的所有值填充到 ListChilds 中。

    【讨论】:

    • 谢谢你的建议,我试了一下,现在请求在服务器收到,但是ListChilds的参数为空
    • 对不起,我没有带角度的机器,也无法安装。但我最好的猜测是 obj.ListChild 可能与 MainVM 的 ListChilds 的结构不同。要测试它,只需打印 JSON.stringify(obj.ListChilds) 并检查生成的 JSON。它应该与您的模型具有相似的结构。
    • 客户端和服务器端的结构相同。
    【解决方案2】:

    最后发现是Asp.net Core 2.2的一个bug,我在这里找到了:

    https://github.com/aspnet/AspNetCore/issues/4802

    问题是当我们在子模型中采用 IFormFile 类型时,Asp.net core 2.2 程序开始挂起(如上面的链接所述)。

    作为一种解决方法,我从 ChildVM 中删除以下行:

    public IFormFile Image { get; set; }
    

    并在 MainVM 中添加以下行:

    public List<IFormFile> ListImage { get; set; }
    

    【讨论】:

      猜你喜欢
      • 2015-08-30
      • 2013-04-23
      • 2019-04-28
      • 1970-01-01
      • 2016-12-28
      • 1970-01-01
      • 2020-06-02
      • 2014-06-21
      • 2018-03-31
      相关资源
      最近更新 更多