【问题标题】:.net Core Angular 6 API [FromBody] is null when published.net Core Angular 6 API [FromBody] 发布时为空
【发布时间】:2018-11-12 12:04:27
【问题描述】:

我的 API 有问题。

因此,当我在我的 IDE 中并通过 ng servedotnet run 运行我的代码时,一切正常,并且我在后端获得了值。 但是当我发布我的代码并在我的服务器上尝试它时,FromBody var 为空。

同样在 Chrome 开发工具中,我看到了请求有效负载中的值。

为什么是 Oberservable? 因为那是我的结果,我不知道其他方式。

API 损坏

在 IDE 中工作,但在发布时不工作

我的打字稿

signupUser(user: User): Observable<boolean> {
  return this.http.post<boolean>('api/user/signupuser', user);
}

我的 C#

[HttpPost]
public bool SignUpUser([FromBody]User user)
{
    return UserBusinessLayer.SignUpUser(user);
}

工作 API

在 IDE 中工作和发布时

打字稿

loginUser(email: string, password: string): Observable<boolean> {
  return this.http.post<boolean>('api/user/loginuser', {email: email, password: password});
}

C#

[HttpPost]
public bool LoginUser([FromBody]User user)
{
    return UserBusinessLayer.LoginUser(user);
}

【问题讨论】:

标签: c# angular api http asp.net-core


【解决方案1】:

通过测试发现异常。

原来User 对象总是null,因为它不能被强制转换

我在对象中有一个电话号码,存储为int。但是这个数字有太多的数字。

所以我改用string,现在一切正常。

【讨论】:

    猜你喜欢
    • 2018-09-24
    • 2021-04-19
    • 1970-01-01
    • 2018-11-06
    • 1970-01-01
    • 2018-12-26
    • 2019-01-31
    • 1970-01-01
    • 2020-02-08
    相关资源
    最近更新 更多