dotnet/aspnetcore

Binding Dictionary<int, object> throws FormatException when no data is provided (except RequestVerificationToken)

vinklerradomir opened this issue · 2 comments

Describe the bug

Using asp.NET CORE 3.0, when posting a form with no data except __RequestVerificationToken, and binding Dictionary<int, object>, POST returns 500 with an exception "FormatException: Input string was not in a correct format."

To Reproduce

Steps to reproduce the behavior:
Using Asp.NET CORE 3.0 razor pages
Antiforgery is enabled

This is the razor view:

<form method="post">
    <button>Post</button>
</form>

This is the model:

public void OnPost(Dictionary<int, string> dict) {
}

When posting the form, exception ""FormatException: Input string was not in a correct format." is thrown.

Expected behavior

I expect that the bound property would be null (or an empty dictionary).

Thanks for contacting us, @vinklerradomir.
@pranavkm can you please give this a try? Most probably this will be something we will fix in 5.0, unless something really very severe comes out of this.

Like you noted, the antiforgery form field is posted and is attempted to be model bound as part of this request. You could consider specifying a name using one of the model binding attributes - https://docs.microsoft.com/en-us/dotnet/api/microsoft.aspnetcore.mvc.modelbinderattribute.name?view=aspnetcore-3.0 - to constraint what prefixes are being bound.

Closing as the behavior is by design.