C# HttpClient.SendAsync到Web API参数null

C# HttpClient.SendAsync到Web API参数null,c#,asp.net-core-webapi,C#,Asp.net Core Webapi,我将这个成员对象从前端发送到API,但参数变为null 我已经看到了关于参数始终为null的其他问题,但我的问题是它并不总是为null,它在大多数情况下都有效,但如果我尝试向成员中添加更多(子项),API[FromBody]参数将变为null 这是我的HttpClient到API的转换 private async Task SendToApi(HttpMethod方法、string requestUrl、T responseBody、string headerXData、bool extende

我将这个成员对象从前端发送到API,但参数变为null

我已经看到了关于参数始终为null的其他问题,但我的问题是它并不总是为null,它在大多数情况下都有效,但如果我尝试向成员中添加更多(子项),API[FromBody]参数将变为null

这是我的HttpClient到API的转换

private async Task SendToApi(HttpMethod方法、string requestUrl、T responseBody、string headerXData、bool extendedRequest)
{
var token=await_httpContextAccessor.GetAccessToken();
var request=GetHttpRequestForApacall(方法、请求URL、令牌、响应库、头扩展数据、扩展请求);
使用(var cts=new System.Threading.CancellationTokenSource())
{
cts.CancelAfter(TimeSpan.FromSeconds(extendRequest?\u extendedTimeout:\u timeout));
/*
var something=wait request.Content.ReadAsByteArrayAsync();
var jsonStr=Newtonsoft.Json.JsonConvert.DeserializeObject(Encoding.UTF8.GetString(something));
在这里工作,可以反序列化到MemberDtoModel
*/
返回wait-wait-HttpClient.SendAsync(请求,cts.Token);
}
}
已编辑

protected HttpRequestMessage GetHttpRequestForApiCall<T>(HttpMethod method, string requestUrl, string bearerToken, T responseBody, string headerXData, bool extendRequest)
        {
            var request = new HttpRequestMessage(method, requestUrl);
            request.Headers.Add("Ocp-Apim-Subscription-Key", ApiSubscriptionKey);
            request.Headers.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", bearerToken);
            if (!string.IsNullOrWhiteSpace(headerXData))
            {
                request.Headers.Add("x-data", headerXData);
            }

            request.Content = CreateContent(method, responseBody);

            if (extendRequest)
            {
                request.Headers.Add("extendRequest", "true");
            }
            return request;
        }

        private HttpContent CreateContent<T>(HttpMethod method, T responseBody)
        {
            if (responseBody == null)
            {
                return null;
            }

            switch (responseBody)
            {
                case string stringBody:
                    if (!string.IsNullOrWhiteSpace(stringBody))
                    {
                        return new StringContent(stringBody, Encoding.UTF8, "application/json");
                    }
                    break;

                case ApiCommunicationContentModel contentBody:
                    if (method != HttpMethod.Get && contentBody?.Body != null)
                    {
                        var content = new ByteArrayContent(contentBody.Body);
                        content.Headers.ContentLength = contentBody.ContentLength;
                        content.Headers.Add("Content-Type", contentBody.ContentType);
                        return content;
                    }
                    else if (method != HttpMethod.Get && contentBody?.FileCollection != null)
                    {
                        var content = new MultipartFormDataContent(contentBody.ContentType.Split(';')[1]);
                        foreach (var item in contentBody.FileCollection)
                        {
                            var streamContent = new StreamContent(item.OpenReadStream());
                            streamContent.Headers.ContentLength = item.Length;
                            content.Add(streamContent, item.Name, item.FileName);
                        }
                        return content;
                    }
                    break;

                default:
                    throw new NotImplementedException();
            }
            return null;
        }
受保护的HttpRequestMessage GetHttpRequestForapCall(HttpMethod方法、字符串请求URL、字符串bearerToken、T响应库、字符串头扩展数据、布尔扩展请求)
{
var request=newhttprequestmessage(方法,requestUrl);
添加(“Ocp Apim订阅密钥”,ApiSubscriptionKey);
request.Headers.Authorization=new System.Net.Http.Headers.AuthenticationHeaderValue(“Bearer”,bearerToken);
如果(!string.IsNullOrWhiteSpace(headerXData))
{
request.Headers.Add(“x-data”,headerXData);
}
request.Content=CreateContent(方法,responseBody);
if(扩展请求)
{
添加(“extendRequest”、“true”);
}
返回请求;
}
私有HttpContent CreateContent(HttpMethod方法,T响应库)
{
if(responseBody==null)
{
返回null;
}
开关(响应底座)
{
大小写字符串字符串体:
如果(!string.IsNullOrWhiteSpace(stringBody))
{
返回新的StringContent(stringBody,Encoding.UTF8,“application/json”);
}
打破
案例ApiCommunicationContentModel contentBody:
if(method!=HttpMethod.Get&&contentBody?.Body!=null)
{
var content=newbytearraycontent(contentBody.Body);
content.Headers.ContentLength=contentBody.ContentLength;
添加(“内容类型”,contentBody.ContentType);
返回内容;
}
else if(method!=HttpMethod.Get&&contentBody?.FileCollection!=null)
{
var content=new MultipartFormDataContent(contentBody.ContentType.Split(“;”)[1]);
foreach(contentBody.FileCollection中的var项)
{
var streamContent=新的streamContent(item.OpenReadStream());
streamContent.Headers.ContentLength=项.Length;
添加(streamContent、item.Name、item.FileName);
}
返回内容;
}
打破
违约:
抛出新的NotImplementedException();
}
返回null;
}
这是我的API

[HttpPost(“成员”)]
公共异步任务SaveTemplate([FromBody]MemberDtoModel成员)//此处成员变为null
{
wait_memberService.SaveMember(会员);
返回Ok(true);
}
是因为内容长度吗?有问题的那个有212208个内容长度,但我已经设置了

<requestLimits maxAllowedContentLength="55000000" />


应该如何调试它?怎么了?还有什么其他方法可以在API中获取MemberDtoModel参数呢?请尝试在您的请求中将
内容类型
标题设置为
“application/json”

您的
GetHttpRequestForApacall
是什么?@Rena,我更新了代码,请参见能否共享
MemberDtoModel
类的代码以及从请求正文发送的示例数据?