c# - 发布到 Asp.Net Core 收到空对象
问题描述
我正在从我的 Angular 7 应用程序向 AspNet Core 后端服务器发出 POST 请求。但是,当接收模型时,它是空的。
我正在使用 Postman 进行测试,并且在使用 Postman 时它可以工作。但是从客户端应用程序发送时它不起作用。
这是我的控制器和控制器基础:
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
}
}
这是我的 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'))
);
}
从我的客户端应用程序发送的对象使用的是驼峰式属性,而后端的模型使用的是 pascal-case。在 Postman 中,这不会影响任何事情,但以防万一。
我真的很困惑为什么从邮递员那里打电话时它正在工作。但是来自客户端的每次测试,我都会收到一个空对象。
编辑#1 我从我的 Experience 类中删除了所有字段,只使用了名称和描述,它起作用了,我只是发现如果我有一个复杂的属性,我就会遇到这个问题。我添加了一个默认构造函数,初始化属性但仍然遇到同样的问题。请注意,我只有在使用 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; }
}
提前致谢
解决方案
推荐阅读
- python - 将任意角度转换为区间 [ -pi , pi ]
- ruby-on-rails - 为什么 Rails 4.2 应用程序在运行 rake db:migrate 任务期间使用 Ansible 在 Ubuntu 服务器上部署时创建具有 root 权限的日志文件
- javascript - __doPostback Javascript 函数导致连续回发
- asp.net-core - 如何使用本地 Active Directory 在 .Net Core 3.1 Razor Pages Web 应用程序中对用户进行身份验证
- javascript - 有没有办法在 keystone 初始化后在运行时创建 Keystone 列表?
- javascript - 忽略 JavaScript 副本上的子元素
- mongodb - 使用 $group 进行聚合以获取第一个元素(有条件)
- javascript - 在 REACT 中提升消息
- ansible - Ansible playbook 为目录中的每个子目录运行命令
- pandas - 根据 pandas 中的 groupby shift 创建新列