Asp.net core 无法从API 6获取错误消息

Asp.net core 无法从API 6获取错误消息,asp.net-core,angular6,Asp.net Core,Angular6,我使用以下函数发布给定类的对象 public Post<T>(object: T, url: string, httpOptions: {}): Observable<T> { return this.httpClient.post<T>(`${environment.apiEndpoint}` + url, object, httpOptions) .pipe( catchError(this.handleError) ); 问题是,当AP

我使用以下函数发布给定类的对象

public Post<T>(object: T, url: string, httpOptions: {}): Observable<T> {
return this.httpClient.post<T>(`${environment.apiEndpoint}` + url, object, httpOptions)
  .pipe(
    catchError(this.handleError)
  );
问题是,当API返回错误时(我可以看到在网络选项卡中包含错误消息),它告诉我响应中没有主体。API返回一个HttpResult,其中错误消息被添加到响应字段

return new HttpResult { StatusCode = HttpStatusCode.Conflict, Response = "Error message"}
我使用以下函数来处理错误

private handleError(error: HttpErrorResponse) {
if (error.error instanceof ErrorEvent) {
  // A client-side or network error occurred. Handle it accordingly.
  console.error('An error occurred:', error.error.message);
}
else {
  console.log(error);
  console.error(
    `Backend returned code ${error.status}, ` +
    `body was: ${error.error}`);
}
console.log(error);
return throwError(
  error.error)
};
它是Angular 6和ServiceStack API。
如有任何建议,将不胜感激

仅供参考,最好在ServiceStack中返回结构化错误响应,您可以使用:

HttpError.Conflict("Error message");
这将使您在使用时捕捉到它:

但从这个答案来看,它表明错误主体应该可以通过以下方式访问:

this.httpClient
  .get("data-url")
  .catch((err: HttpErrorResponse) => {
    // simple logging, but you can do a lot more, see below
    console.error('An error occurred:', err.error);
  });

仅供参考,最好在ServiceStack中返回结构化错误响应,您可以使用:

HttpError.Conflict("Error message");
这将使您在使用时捕捉到它:

但从这个答案来看,它表明错误主体应该可以通过以下方式访问:

this.httpClient
  .get("data-url")
  .catch((err: HttpErrorResponse) => {
    // simple logging, but you can do a lot more, see below
    console.error('An error occurred:', err.error);
  });

我明白了,我来看看ServiceClient。然而,你给出的解决方案,不就是我所做的吗?我只是用一个函数来代替?我得到了HttpErrorResponse,但里面没有响应。在我的具体案例中,我得到的是“409冲突”,而不是“设备已经存在”。409冲突听起来像是来自状态描述,而不是响应主体。无法与您看到的错误对话,但从我看到的一切来看
HttpErrorResponse.error
应该是,当我返回HttpResult{StatusCode=HttpStatusCode.Accepted,Response=“Added”}我得到了响应,但当我尝试对409执行相同操作时,我只得到了冲突,这对我来说没有任何意义。。现在看看你们的客户机,我想我将开始使用它。你们如何让客户机为模板功能工作?当我将该对象传递到帖子中时,它不起作用。@AtleKristiansen我不知道您指的是什么,请发布一个新问题,详细说明问题所在,包括原始HTTP请求/响应头。我明白了,我将查看ServiceClient。然而,你给出的解决方案,不就是我所做的吗?我只是用一个函数来代替?我得到了HttpErrorResponse,但里面没有响应。在我的具体案例中,我得到的是“409冲突”,而不是“设备已经存在”。409冲突听起来像是来自状态描述,而不是响应主体。无法与您看到的错误对话,但从我看到的一切来看
HttpErrorResponse.error
应该是,当我返回HttpResult{StatusCode=HttpStatusCode.Accepted,Response=“Added”}我得到了响应,但当我尝试对409执行相同操作时,我只得到了冲突,这对我来说没有任何意义。。现在看看你们的客户机,我想我将开始使用它。你们如何让客户机为模板功能工作?当我将对象传递到帖子中时,它不起作用。@AtleKristiansen我不知道您指的是什么,请发布一个新问题,并提供问题所在的详细信息,包括原始HTTP请求/响应头。
this.httpClient
  .get("data-url")
  .catch((err: HttpErrorResponse) => {
    // simple logging, but you can do a lot more, see below
    console.error('An error occurred:', err.error);
  });