我正在通过Angular 6发布请求将数据发送到.Net Web Api,但是所有值都以null的形式到达服务器。有什么想法吗?
角形帖子:
var vacancy = {
'number': this.vacancyForm.get('number').value,
'requester': this.vacancyForm.get('requester').value,
'date': this.vacancyForm.get('date').value,
'position': this.vacancyForm.get('position').value,
'replacement': this.vacancyForm.get('replacement').value,
'discharge_date': this.vacancyForm.get('discharge_date').value,
'candidate': this.vacancyForm.get('candidate').value,
'contract_type': this.vacancyForm.get('contract_type').value,
'working_day': this.vacancyForm.get('working_day').value,
'annual_salary': this.vacancyForm.get('annual_salary').value,
'business_division': this.vacancyForm.get('business_division').value,
'company': this.vacancyForm.get('company').value,
'workplace': this.vacancyForm.get('workplace').value,
'personal_division': this.vacancyForm.get('personal_division').value,
'department': this.vacancyForm.get('department').value,
'cost_center': this.vacancyForm.get('cost_center').value,
'workplace_address': this.vacancyForm.get('workplace_address').value
}
const headers = new HttpHeaders().set('Content-Type', 'application/x-www-form-urlencoded; charset=UTF-8');
var postReturn = this.http.post<any>(environment.apiEndpoint + "/Api/Vacancy", JSON.stringify(vacancy), { headers })
.subscribe(
(val) => {
console.log('POST call successful value returned in body',
val);
},
response => {
console.log('POST call in error', response);
},
() => {
console.log('The POST observable is now completed.');
});
.Net Post Controller中的方法:
[HttpPost]
public IEnumerable<string> Post(Vacancy vacancy)
{
if (vacancy == null)
{
throw new System.ArgumentNullException(nameof(vacancy));
}
return new string[] { "I'm doing just nothing but returning a string." };
}
.Net空缺模型类:
public class Vacancy
{
public int number { get; set; }
public string requester { get; set; }
public DateTime date { get; set; }
public string position { get; set; }
public string replacement { get; set; }
public DateTime discharge_date { get; set; }
public string candidate { get; set; }
public string contract_type { get; set; }
public string working_day { get; set; }
public string annual_salary { get; set; }
public string business_division { get; set; }
public string company { get; set; }
public string workplace { get; set; }
public string personal_division { get; set; }
public string department { get; set; }
public string cost_center { get; set; }
public string workplace_address { get; set; }
}
我也尝试删除JSON.stringify,但结果相同,Vacancy对象始终接收空值。
任何帮助将不胜感激。
谢谢。
答案 0 :(得分:2)
即使使用[Frombody],对.NET Core Web API进行POST时,我也遇到类似的问题,即得到空对象。原来我的C#模型与Angular模型不完全匹配。这些属性之一在Angular模型端为空,而在C#端则为空。更新模型以使其匹配后,它可以正常工作。
[HttpPost]
public IActionResult Post([FromBody] CustomerAccount customerAccount)
{
// Code goes here
}
然后Angular边看起来像这样:
addCustomerAccount(customerAccount: CustomerAccount): Observable<CustomerAccount>
{
return this.http.post<CustomerModel>(`${environment.rootAPI}/Customer`, customerAccount)
}
这是我的C#模型(将其设置为可空后可以映射查找)
public class CustomerAccount
{
public long AccountNumber { get; set; }
public bool? IsPrimaryCustomer { get; set; }
public Customer Customer { get; set; }
}
答案 1 :(得分:0)
尝试将Content-Type设置为:application / json; charset = utf-8