简体   繁体   English

发布到Asp.Net Core收到空对象

[英]Posting to Asp.Net Core receives null object

I'm making a POST request from my Angular 7 application to a AspNet Core backend server. 我正在从Angular 7应用程序向AspNet Core后端服务器发出POST请求。 However when receiving the model, it's null. 但是,在接收模型时,它为null。

I'm testing using Postman, and it works when using Postman. 我正在使用邮递员进行测试,并且在使用邮递员时可以使用。 But it's not working when sending from the client app. 但是,从客户端应用程序发送时,它不起作用。

Here is my controller and controller base: 这是我的控制器和控制器库:

public class BaseController : ControllerBase {
    public readonly ILogger _logger;
}
[Route("api/[controller]")]
public class ExperienceController : BaseController {
    [HttpPost, Route("Create")]
    public async Task<IActionResult> Create([FromBody] Experience experience){
      // code ommited
    }
}

Here my service.ts code 这是我的service.ts代码

const httpOptions = {
  headers: new HttpHeaders({ 'Content-Type': 'application/json' })
};
// some code omitted

saveExperience(experience: Experience): Observable<Experience> {
  let url = `${this.baseUrl}/Experience/Create`;
  return this.http.post<Experience>(url, experience, httpOptions).pipe(
    tap((newExp: Experience) => this.log(`Created experience ${newExp}`)),
    catchError(this.handleError<Experience>('saveExperience'))
  );
}

客户要求

The object being sent from my client app is using camel-case properties, and the model for the backend is using pascal-case. 从我的客户端应用程序发送的对象使用驼峰式案例属性,后端模型使用pascal-case。 In Postman this is not affecting anything, but just in case. 在Postman中,这不会影响任何事情,但以防万一。 邮递员要求

I'm really confused on why when calling from Postman it's working. 我真的很困惑为什么从邮递员那里打电话时它能正常工作。 But every test from the client, I'm receiving a null object. 但是从客户端进行的每次测试,我都会收到一个空对象。

EDIT #1 I removed all fields from my Experience class and used only name and description and it worked, I just found that I'm getting this issue if I have a complex property. 编辑#1我从体验类中删除了所有字段,仅使用名称和描述,并且它起作用了,我只是发现如果我有一个复杂的属性,就会遇到这个问题。 I have added a default constructor, initializing the property but still getting the same issue. 我添加了一个默认的构造函数,初始化了属性,但仍然遇到相同的问题。 Please note that I'm getting this issue only when using the Angular app, if I use Postman it's working 请注意,只有在使用Angular应用程序时我才会遇到此问题,如果我使用Postman则可以正常工作

public class Experience : ExperienceBase {
    public string Name { get; set; }
    public string Description { get; set; }

    // this guy here is causing the issue
    public LocationDetails LocationDetails { get; set; } 

}

Thanks in advance 提前致谢

First you dont need to set the HttpHeader to application/json so you remove the HttpHeader so your code would be like this 首先,您不需要将HttpHeader设置为application/json因此您删除了HttpHeader,因此您的代码将如下所示

saveExperience(experience: Experience): Observable<Experience> {
  let url = `${this.baseUrl}/Experience/Create`;
  return this.http.post<Experience>(url, experience).pipe(
    tap((newExp: Experience) => this.log(`Created experience ${newExp}`)),
    catchError(this.handleError<Experience>('saveExperience'))
  );
}

Please let me know if you still have any problem 如果您还有任何问题,请告诉我

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM