繁体   English   中英

C# Rest api - post list object is always null

[英]C# Rest api - post list object is always null

我正在使用此代码将一些 object 发布到我的 Rest API:

public static string Post<T>(string uri, T data, string contentType = "application/json", string method = "POST")
{
    byte[] dataBytes = Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(data));
    string str = JsonConvert.SerializeObject(data);

    HttpWebRequest request = (HttpWebRequest)WebRequest.Create(uri);
    request.AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate;
    request.ContentLength = dataBytes.Length;
    request.ContentType = contentType;
    request.Method = method;

    using (Stream requestBody = request.GetRequestStream())
    {
        requestBody.Write(dataBytes, 0, dataBytes.Length);
    }

    using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
    using (Stream stream = response.GetResponseStream())
    using (StreamReader reader = new StreamReader(stream))
    {
        return reader.ReadToEnd();
    }
} 

当发布一个简单的 object 时,代码工作得很好:

[Route("api/Name/{ActivationKey}")]
public async Task<HttpResponseMessage> Post([FromBody] SomeKindOfObject value, string ActivationKey)
{
    Some code...
}

但是,当我尝试发布相同 object 的列表时,我在null中得到 null:

[Route("api/Name/{ActivationKey}")]
public async Task<HttpResponseMessage> Post([FromBody] List<SomeKindOfObject> value, string ActivationKey)
{
    Value is always null.
}

什么可能导致这种情况,我该如何解决?

尝试

public async Task<HttpResponseMessage> Post([FromBody] SomeKindOfObject[] value, string ActivationKey)

反序列化可能与其目标容器类型相反,即。 列表 vs 数组 vs 集合。

显然问题是我尝试发布的列表太大(超过 15k 行),我所做的是将列表拆分为较短的列表并单独发布每个部分并且它有效。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM