简体   繁体   English

C# .NET Core 2.2 API not receiving parameter object being sent from Angular front end

[英]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.

相关问题 如何在 c# webapi 中处理从前端(网站)发送的 excel 文件? - How to process and excel file that is being sent from front end(website) in c# webapi? how do i pass a datatable from a .net core 5.0 web api to an angular front end as json? - how do i pass a datatable from a .net core 5.0 web api to an angular front end as json? 从Angular将Json对象解析为C#ASP.Net Core Web API - Parse Json object from Angular to C# ASP.Net Core Web API 尝试将对象从asp.net Web API 2返回到Angular前端时出现CORS错误 - CORS Error when trying to return an Object from asp.net Web API 2 to Angular Front end 如何从前端(Angular)向C#.net datetime(webAPI)发送日期? - How can I send a date from my front end (Angular) to C# .net datetime (webAPI)? .net core web app JQuery AJAX前端没有从后端.net core api获取数据 - .net core web app JQuery AJAX Front end not getting data from Backend .net core api 如何使用angular作为前端将文件从C#代码上传到azure,并通过api将文件传递给api控制器 - How to upload file to azure from C# code by using angular as front end and passing file through the api to api controller .NET CORE 2.2 MVC C#中的MapRoutes - MapRoutes in .NET CORE 2.2 MVC C# 在c#后端调用api时角度前端获取错误 - Angular front-end getting error when calling api on c# back-end ASP.Net Core 6 HttpGet API ==&gt; 如何将 [FromQuery] 参数绑定到 C# object 数据类型 - ASP.Net Core 6 HttpGet API ==> How to bind [FromQuery] parameter to C# object data type
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM