このコードを 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);
...失敗します。エラー メッセージはありません。DataTable (_dtUsage) は、JsonConvert.SerializeObject() メソッドで問題なく json にシリアル化されます。「大量」のデータ (数千レコード) があるだけです。クライアントの PostAsync() への呼び出しに到達しないため、サーバーの Post メソッドに到達することはありません。
些細な量以上のデータを送信するときにこれを成功させる回避策はありますか? それほど多くのデータをネットワーク経由で渡すのはあまり好きではありませんが (現在、クライアントとサーバーは両方ともローカルで実行されていますが、デプロイ後の状況を考えてください)、もう 1 つのオプションは、両方のクライアントから同じストアド プロシージャを実行することです (そこに Excel スプレッドシート ファイルを生成する) およびサーバーから (データを HTML に変換する)。それは「Catch-22」状況の 1 つであると思われます (「そうする場合は差し押さえられ、そうでない場合は隔離されます」)。
アップデート
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)
...「引数 1: 'string' から 'int' に変換できません」などの他のコンパイル エラーが発生する