gpt4 book ai didi

c# - 如何使用 DictionaryModelBinder?

转载 作者:行者123 更新时间:2023-12-01 17:35:52 25 4
gpt4 key购买 nike

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

注意

关于c# - 如何使用 DictionaryModelBinder?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57226534/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com