ASP.NET Core 2.2 DictionaryModelBinder
[HttpGet("{id}", Name = "GetValue")]
public async Task<IActionResult> GetValue(
[ModelBinder(BinderType=typeof(DictionaryModelBinder<string,string>))]
IDictionary<string, string> id)
{
return Ok();
}
Error:
InvalidOperationException: Multiple constructors accepting all given argument types have been found in type 'Microsoft.AspNetCore.Mvc.ModelBinding.Binders.DictionaryModelBinder`2[System.String,System.String]'. There should only be one applicable constructor.
After some source code analyze, I found the way to use DictionaryModelBinder.
[HttpGet("GetValue")]
public async Task<IActionResult> GetValue([FromQuery] IDictionary<string, string> id)
{
return Ok();
}
and the request should be
api/GetValue?id.Key1=Value1&id.Key2=Value2&id.Key3=Value3
(OR)
api/GetValue?id[Key1]=Value1&id[Key2]=Value2&id[Key3]=Value3
Note
ModelBinderAttribute. This is added by defaultFromQueryAttribute for the parameterIf you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With