简体   繁体   English

托管在 azure 上的 webapi 总是收到一个空列表

[英]webapi hosted on azure always receives an empty list

So here is the scenario.所以这是场景。 Im sending a list of 14 objects to a webapi hosted on azure.我将 14 个对象的列表发送到托管在 azure 上的 webapi。 But it always receives an empty list.但它总是收到一个空列表。 Im calling the webapi from backend like so.我像这样从后端调用 webapi。

using (HttpClient objHttpCLient = new HttpClient())
                {
                    HttpResponseMessage objMessage = objHttpCLient.PostAsync(new Uri(strUrl), new StringContent(JOContent.ToString(), Encoding.UTF8, "application/JSON")).Result;
                    var ResponseClientConfiguration = objMessage.Content.ReadAsStringAsync();
                    sw.WriteLine(objMessage.StatusCode.ToString());
                    if (objMessage.IsSuccessStatusCode)
                    {
                        sw.WriteLine(ResponseClientConfiguration.Result);
                        objResponse = JObject.Parse(ResponseClientConfiguration.Result);
                        strConfigurationJSON += objResponse["ConfigurationJson"].ToString();
                    }
                }

Now this works perfectly when the webapi is hoated on my local environment.现在,当 webapi 在我的本地环境中运行时,这可以完美运行。 it receives all 144 objects, but as soon as it is hosted on azure, it always receives an empty lit.它接收所有 144 个对象,但是一旦它托管在 azure 上,它总是收到一个空的点亮。

So here is the solution.所以这是解决方案。 I still dont know why the above doesnt work when the webApi project is hosted on azure but does work when hosted locally or a normal webserver.我仍然不知道为什么当 webApi 项目托管在 azure 上时上述方法不起作用,但在本地托管或普通网络服务器时却起作用。 The workaround is to create a class to coverthe list of objects ie the list of objects will now be a property of this class.解决方法是创建一个类来覆盖对象列表,即对象列表现在将成为此类的一个属性。

Before the parameter I was expecting on the webApi was as follows之前我在webApi上期待的参数如下

public JObject Validate(List<DateTime> lstDates)

After its this在这之后

public JObject Validate(DateLst objDates)

where the DateLst Class structure is as follows其中 DateLst 类结构如下

public class DateLst 
{

  public List<DateTime> lstDates{ get; set;}

}

Now why this works and the former doesnt on azure is still a mystery.现在为什么这有效而前者在 azure 上无效仍然是个谜。

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

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