c# - 如何使用 DictionaryModelBinder?

标签 c# asp.net asp.net-core

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/

相关文章:

c# - 单声道.rsp文件

c# - Monotouch 如何在 C# 中从 NSArray 检索 objectAtIndex

asp.net - 哪个最快?数据检索

javascript - AJAX 过滤 webgrid

asp.net-core - Xunit 无法使用 Visual Studio Team Services Build VNext 工作

asp.net-core - ASP.NET Core DI 直接访问容器

c# - 在直接调用委托(delegate)的情况下如何缓解 "Access to modified closure"

c# - 窗体 : navigate TreeView in BackgroundWorker thread

c# - Asyncfileupload 在 webusercontrol 中不起作用

c# - .NET Core 2.0 身份和 jwt?