ASP.NETの画面から送信できるアイテム数を指定するサンプルです。言語は、VB.NETです。
MaxHttpCollectionKeys
- MaxHttpCollectionKeysは、送信するアイテム数を指定できます。
- Web.configファイルのappSettings内に記述します。
- デフォルト値は1000です。
- 以下は、MicrosoftのASP.NET appSettings Elementのリンクです。
(リンク先でMaxHttpCollectionKeysで検索します)
https://docs.microsoft.com/en-us/previous-versions/aspnet/hh975440(v=vs.120) - 上記のリンク先では、この値を大きくしすぎると、セキュリティ上のリスクが発生する可能性があると書いてあります。
以下は、Web.configファイルのMaxHttpCollectionKeysのサンプルです。
<configuration>
<appSettings>
<add key="aspnet:MaxHttpCollectionKeys" value="1" />
<add key="webpages:Version" value="3.0.0.0"/>
<add key="webpages:Enabled" value="false"/>
<add key="ClientValidationEnabled" value="true"/>
<add key="UnobtrusiveJavaScriptEnabled" value="true"/>
</appSettings>
</configuration>
3行目は、keyに「aspnet:MaxHttpCollectionKeys」、valueに「1」を追加しました。
送信するアイテム数は、1つのみ可能という意味になります。
ビュー
値を送信するビューです。ファイル名はindex.vbhtmlです。
@ModelType new0901.ActionForm1
@Code
Layout = Nothing
End Code
<!DOCTYPE html>
<html>
<head>
</head>
<body>
@Using Html.BeginForm("Display1", "Test1", FormMethod.Post, New With {.id = "form1"})
@<p>@Html.TextBoxFor(Function(model) model.text1, New With {.maxlength = 5})</p>
@<p>@Html.TextBoxFor(Function(model) model.text2, New With {.maxlength = 5})</p>
@<input type="submit" id="button1" value="ボタン" />
End Using
</body>
</html>
11,12行目は、2つのテキストボックスがあります。
上記の状態でボタンを押すと以下のエラーになります。
要求(リクエスト)から読み取ったフォーム、クエリ文字列、またはポストされたファイルのアイテム数が既に最大数に達しました。要求コレクションの最大許容カウントを現在の値 1 から変更するには、"aspnet:MaxHttpCollectionKeys" 設定を変更します。詳細については、http://go.microsoft.com/fwlink/?LinkId=238386 を参照してください。
解決法
画面のForm内のテキストボックスの数は2なので、MaxHttpCollectionKeysの値を1から2にするとエラーは発生しなくなります。
(参考)コントローラ
コントローラです。
Namespace Controllers
Public Class Test1Controller
Inherits Controller
Function Index() As ActionResult
Return View()
End Function
Function Display1(actionform As ActionForm1) As ActionResult
Return View("view2", actionform)
End Function
End Class
End Namespace
(参考)フォーム
フォームです。テキストボックスの値を受けます。
Public Class ActionForm1
Public Property text1 As String
Public Property text2 As String
End Class
関連の記事