0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

ASP .NET Core Tips - 配列データのformをpostする際の注意点

Last updated at Posted at 2025-04-21

概要

ASP .NET CoreでWebアプリの開発していた際に起こったエラーの解決方法の備忘録。

環境

Visual Studio 2022
ASP .NET Core

発生したエラー

配列データを表形式で表示して、ユーザーにデータを入力してもらうWebアプリを開発。
列数が設定によって増減するんですが、列数が多くなると、Submitした際になぜかBad Request(400)が返ってくる現象が発生。
検索してみると、キャッシュやサイズの問題ではという情報もありましたが、今回は4~5桁の数字なのでデータサイズの問題ではなさそう。キャッシュをクリアしてもエラーが発生する状況。

理由と対処方法

ASP .NET Coreでは、formに対して様々な制限が設定されているとのこと。

RequestFormLimitsAttribute クラス

今回引っかかった制限は、データサイズではなく、inputタグの数。
postされたデータ数がValueCountLimitプロパティによる上限を超えたことにより、エラーとなっていました。
ControllerのPOSTメソッドに上限を変更するアトリビュートを追記することで、エラーを回避できました。

[HttpPost]
[RequestFormLimits(ValueCountLimit = 2048)]
[ValidateAntiForgeryToken]
public async Task<IActionResult> Create(XXXXViewModel xxxxDatas)
{
    ・・・
}

デフォルトが1024だそうです。
inputタグの数なので、表示されていない項目(type=hiddenとなっているinputタグ)も対象となっています。
配列データを扱うときは、inputタグの数がリミットを超えないかを確認しなきゃですね・・・。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?