[英]C# .NET Core 2.2 API not receiving parameter object being sent from Angular front end
I am trying to make a post request to my C# ASP.NET Core 2.2 backend from Angular.我正在尝试从 Angular 向我的 C# ASP.NET Core 2.2 后端发出发布请求。 The API is being hit, but my parameter is not being received by the API and is being instead initialized as null. API 被击中,但我的参数没有被 API 接收,而是被初始化为 null。
When I check the network tab my parameters are being sent and are matching the parameter type in the function, but it is still showing as null.当我检查网络选项卡时,我的参数正在发送并且与 function 中的参数类型匹配,但它仍然显示为 null。 I have also tried adding a [FromBody]
attribute to the parameter but that makes no difference.我也尝试向参数添加[FromBody]
属性,但这没有区别。
Why is the parameter not received on the backend?为什么后端收不到参数?
Backend API structure后端 API 结构
[HttpPost]
public void GetCanadaPostRates(ShippingRateModel info)
{
// this is being hit but info is showing as null
// do something
}
Frontend service:前端服务:
export class ShippingService {
private apiUrl = environment.apiBaseUrl + '_3rdPartyShipping/';
constructor(private http: HttpClient) {}
getCanadaPostShippingQuote(info) {
return this.http.post<any>(this.apiUrl + 'GetCanadaPostRates', info);
}
}
Frontend service call:前端服务调用:
this.shippingService.getCanadaPostShippingQuote(shippingRates).subscribe(res => {})
Frontend model:前端 model:
export class ShippingRateModel {
WeightKg: number;
OriginPostalCode: string;
DestPostalCode: string;
}
Backend model:后端 model:
namespace Server.Models
{
public class ShippingRateModel
{
float WeightKg { get; set; }
string OriginPostalCode { get; set; }
string DestPostalCode { get; set; }
}
}
Screenshot of network request:网络请求截图:
Your issue is with accessibility of members in the deserialization of the backend type.您的问题是在后端类型的反序列化中成员的可访问性。
define your properties of that type to be public
— that should propagate those members with the values in the request payload.将该类型的属性定义为public
的——应该使用请求有效负载中的值传播这些成员。
namespace Server.Models
{
public class ShippingRateModel
{
public float WeightKg { get; set; }
public string OriginPostalCode { get; set; }
public string DestPostalCode { get; set; }
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.