【问题标题】:Angular HttpClient not returning integer property with value of 0Angular HttpClient 不返回值为 0 的整数属性
【发布时间】:2020-02-10 02:01:32
【问题描述】:

因此,当我使用 Angular HttpClient 从 Nativescript + Angular + TypeScript 应用程序调用 C# 内置的 .Net WebAPI 时,由于某种原因,返回的对象应包含整数属性“UserState”,但没有该属性如果从 WebAPI 返回的值为 0,则完全没有。

如果该值为 1 或更大,则属性“UserState”在 HttpClient 响应对象中可见。

我可以理解 NULL 属性未映射到 JSON 响应中(与下面的“名称”属性相同),但 0 的整数仍然是有效的属性值,看起来很奇怪它的行为就像 NULL价值。

我已经进行了很多搜索,但是很难将其输入到搜索词中,而且我还没有找到与此相关的任何内容。

WebAPI - 用户模型:

public class User
{
    public int Id { get; set; }
    public int UserState { get; set; }
    public string Name { get; set; }
}

WebAPI - System.Web.Http.IHttpActionResult > 内容(为简单起见以 json 显示)

{
    Id: 123456,
    UserState: 0,
    Name: null,
}

应用程序 - 用户界面

export interface User {
    Id: number,
    UserState: number,
    Name: string,
}

App - HttpClient 发布

return this.http.post<User>(url, body, { headers: headers })

应用程序 - 发布响应(如果 UserState 为 0)

{
    Id: 123456,
}

应用程序 - 发布响应(如果 UserState 为 1)

{
    Id: 123456,
    UserState: 1,
}

【问题讨论】:

  • 您的后端(webapi)似乎存在一些问题,因为如果为零,则不会在 json 有效负载上发送 UserState。可以肯定的是,您是否使用 Chrome 开发工具检查了网络选项卡上的响应?

标签: angular post integer httpclient undefined


【解决方案1】:

在您的订阅中进行比较时,请确保您使用的是“==”运算符而不是“=”运算符。

【讨论】:

    猜你喜欢
    • 2013-02-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-10-20
    相关资源
    最近更新 更多