【问题标题】:return ObjectResult from API and use Status Code in angular从 API 返回 ObjectResult 并以角度使用状态代码
【发布时间】:2019-01-29 09:57:45
【问题描述】:

在我的 .Net Core API 中,我返回 ObjectResult

[HttpGet("[Action]")]
    public ObjectResult GetBList()
    {
        List<PendingListBWise> BList = new List<PendingListBWise>();
        try
        {
            BList = GetBWiseList();
            return StatusCode(202, BList);
        }
        catch (Exception ex)
        {
            return StatusCode(500, new ResultSet() { Message = ex.Message, StatusCode = 500 });
        }
        finally
        {
        }
    }

现在在我的角度组件中,如果状态码不是 202,我想打印消息。 (更新 1)

 GetBList() {
this.ListLoader = true;
var url = this.baseurl + 'api/GeneratePDF/GetList/';
this.http.get<PendingListBWise[]>(url)
  .toPromise()
  .then(result:HttpResponse<PendingListBWise[]> => {
    this.BList = result;
    this.ListLoader = false;
  })
  .catch(error => {
    this.ListLoader = false;
    console.error(error);
  });

}

.then(result:HttpResponse => { } 预期有 0​​-2 个参数,但得到了 3 个

为了在这里获取响应代码,我必须这样做

.subscribe(result:httpResponse => {
    console.log(result);

  }, error => {
    console.error(error);

  });

但是从 PendingListBWise[] 中删除结果的转换,这意味着结果不会被输入到所需的类中,

如何在此处获取数据和状态码?

【问题讨论】:

  • 可以使用泛型,所以改成result: HttpResponse&lt;PendingListBWise[]&gt;,用result.body获取值
  • @user184994 它说预期的 0-2 参数但得到了 3
  • 哪一行给出了这个错误?在上面的示例中,该函数仅使用 2 个参数
  • 检查更新 1,到达那里后有 ,尽管删除它没有帮助

标签: angular asp.net-web-api .net-core


【解决方案1】:

首先,从http.get&lt;PendingListBWise[]&gt;中删除泛型

然后,将泛型移动到HttpResponse

你应该得到类似的结果:

  GetBList() {
    this.ListLoader = true;
    var url = this.baseurl + 'api/GeneratePDF/GetList/';
    this.http.get(url)
      .toPromise()
      .then((result: HttpResponse <PendingListBWise[]>) => {
        this.BList = result;
        this.ListLoader = false;
      })
      .catch(error => {
        this.ListLoader = false;
        console.error(error);
      }); 
  }

【讨论】:

    猜你喜欢
    • 2014-02-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-11-18
    相关资源
    最近更新 更多