我在Web API控制器中获得了以下代码:
[Route("{unit}/{begindate}/{enddate}")]
[HttpPost]
public void Post(string unit, string begindate, string enddate,
[FromBody] string stringifiedjsondata)
{
List<ProduceUsageSPResults> _produceUsageList = JsonConvert.DeserializeObject<List<ProduceUsageSPResults>>(stringifiedjsondata);
string _unit = unit;
string _begindate = String.Format("{0}01", HyphenizeYYYYMM(begindate));
string _enddate = GetEndOfMonthFor(enddate);
string appDataFolder =
HttpContext.Current.Server.MapPath("~/App_Data/");
string htmlStr = ConvertProduceUsageListToHtml(_produceUsageList);
string htmlFilename = string.Format("produceUsage_{0}_{1}_{2}.html",
_unit, _begindate, _enddate);
string fullPath = Path.Combine(appDataFolder, htmlFilename);
File.WriteAllText(fullPath, htmlStr);
}
当通过“ [FromBody]” arg从客户端(Winforms应用程序)仅传递少量(人为)数据时,它可以正常工作,如下所示:
private async Task SaveProduceUsageFileOnServer(string beginMonth, string beginYear, string endMonth, string endYear, DataTable _dtUsage)
{
string beginRange = String.Format("{0}{1}", beginYear, beginMonth);
string endRange = String.Format("{0}{1}", endYear, endMonth);
HttpClient client = new HttpClient {BaseAddress = new Uri("http://localhost:42176")};
string dataAsJson = "[{\"ItemDescription\": \"DUCKBILLS, GRAMPS-EIER 70CT 42#\",\"PackagesMonth1\": 1467}]";
//string dataAsJson = JsonConvert.SerializeObject(_dtUsage);
String uriToCall = String.Format("/api/produceusage/{0}/{1}/{2}", _unit, beginRange, endRange);
var content = new FormUrlEncodedContent(new[]
{
new KeyValuePair<string, string>("", dataAsJson)
});
HttpResponseMessage response = await client.PostAsync(uriToCall, content);
}
但是,如果我反转对dataAsJson的赋值的注释,以便它发送实际数据,如下所示:
//string dataAsJson = "[{\"ItemDescription\": \"DUCKBILLS, GRAMPS-EIER 70CT 42#\",\"PackagesMonth1\": 1467}]";
string dataAsJson = JsonConvert.SerializeObject(_dtUsage);
...它失败; 没有错误信息。使用JsonConvert.SerializeObject()方法将DataTable(_dtUsage)序列化为json就可以了;只是有大量的数据(几千条记录)。永远不会到达客户端对PostAsync()的调用,因此永远不会到达服务器的Post方法。
有一种解决方法可以使它在发送不只是少量数据的情况下成功实现吗?我真的不喜欢通过网络传递这么多的数据(当前客户端和服务器都在本地运行,但是考虑一下部署后的情况),但是另一个选择是从两个客户端执行相同的存储过程(以并从服务器生成Excel电子表格文件)(将数据转换为HTML)。这似乎是那些“ Catch-22”情况中的一种(“如果有则被扣押,如果没有则被隔离”)。
测试出user3093073的想法,我将其添加到Web.config的system.webServer部分:
<security>
<requestFiltering>
<requestLimits>
<headerLimits>
<add header="Content-type" sizeLimit="10000000" />
</headerLimits>
</requestLimits>
</requestFiltering>
</security>
(基于我在这里找到的内容,但仍然无法正常工作...
更接近用户user3093073的答案,我也尝试过此操作:
<requestFiltering>
<requestLimits maxAllowedContentLength="10000000">
<!--<headerLimits>
<add header="Content-type" sizeLimit="100" />
</headerLimits>-->
</requestLimits>
</requestFiltering>
...也无济于事
请注意,我将上面的代码放在站点中的每个Web.config文件中,即:
The Web.config file below the \[ProjectName]\Views folder
The Web.config file below the \[ProjectName] folder
The two Web.config files below the \[ProjectName]\Web.config file, namely "Web.Debug.config" and "Web.Release.config"
...或者是查看其位置的另一种方法:
\PlatypusReports\Web.config
\PlatypusReports\Web.config\Web.Debug.config
\PlatypusReports\Web.config\Web.Release.config
\PlatypusReports\Views\Webconfig
在离开了几天之后又回到原来的位置上,对我而言,现在看来,我一直试图传递的“真实”数据与有效的“伪造/测试”数据不同。
测试数据是一个简单的KeyValuePair的集合。但是,实际数据更为复杂。因此,当我尝试将复杂数据转换为简单的KeyValuePair时,肯定会遇到麻烦。所以代替这个:
new KeyValuePair<string, string>("", dataAsJson)
...我需要这样的东西:
new List<DeliveryPerformanceClass>("", dataAsJson)
...但是那也不起作用;我遇到一些编译错误,例如,“ 'System.Collections.Generic.List'不包含带有2个参数的构造函数' “
如果我删除第一个arg以便:
new List<DeliveryPerformanceClass>(dataAsJson)
...我收到其他编译错误,例如“参数1:无法从'string'转换为'int' “
我相信,由于您已经指定了<requestFiltering>
设置,因此还需要设置maxRequestLength
。正如@brewsky提到的那样,默认情况下只有4MB。检查您的web.config。此设置为2GB
<configuration>
<system.web>
<httpRuntime maxRequestLength="2147483648" />
</system.web>
</configuration>
这样的答案似乎确实解决了类似的问题。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句