Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use DictionaryModelBinder?

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.

like image 470
O.O Avatar asked Sep 12 '26 01:09

O.O


1 Answers

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

  • No need to explicitly use ModelBinderAttribute. This is added by default
  • We need to add FromQueryAttribute for the parameter
like image 115
Vijay Nirmal Avatar answered Sep 13 '26 16:09

Vijay Nirmal



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!