我在 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]”参数仅从客户端(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>
(基于我在这里找到的,但它仍然无法正常工作......
更新 2
更接近用户 user3093073 的答案,我也试过这个:
<requestFiltering>
<requestLimits maxAllowedContentLength="10000000">
<!--<headerLimits>
<add header="Content-type" sizeLimit="100" />
</headerLimits>-->
</requestLimits>
</requestFiltering>
……也无济于事。
更新 3
请注意,我将上面的代码放在站点的每个 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
更新 4
在离开这个几天并回到它之后,现在对我来说似乎很清楚,我试图传递的“真实”数据与假冒/测试数据不同,后者有效。
测试数据是一个简单的 KeyValuePair 的集合。然而,真实数据更为复杂。因此,当我尝试将复杂的数据转换为简单的 KeyValuePair 时,肯定会遇到麻烦。所以代替这个:
new KeyValuePair<string, string>("", dataAsJson)
...我需要这样的东西:
new List<DeliveryPerformanceClass>("", dataAsJson)
...但这也不起作用;我收到几个编译错误,例如“ 'System.Collections.Generic.List' 不包含采用 2 个参数的构造函数”
如果我删除第一个参数,它是:
new List<DeliveryPerformanceClass>(dataAsJson)
...我得到其他编译错误,例如,“ Argument 1: cannot convert from 'string' to 'int' ”