ASP.NET 核心 2.2 DictionaryModelBinder
[HttpGet("{id}", Name = "GetValue")]
public async Task<IActionResult> GetValue(
[ModelBinder(BinderType=typeof(DictionaryModelBinder<string,string>))]
IDictionary<string, string> id)
{
return Ok();
}
错误:
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.
最佳答案
经过一些源代码分析,我找到了使用DictionaryModelBinder
的方法。 .
[HttpGet("GetValue")]
public async Task<IActionResult> GetValue([FromQuery] IDictionary<string, string> id)
{
return Ok();
}
请求应该是
api/GetValue?id.Key1=Value1&id.Key2=Value2&id.Key3=Value3
(或)
api/GetValue?id[Key1]=Value1&id[Key2]=Value2&id[Key3]=Value3
注意
- 无需明确使用
ModelBinderAttribute
.这是默认添加的 - 我们需要添加
FromQueryAttribute
对于参数
关于c# - 如何使用 DictionaryModelBinder?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57226534/