如何根据参数名称前缀从URL查询绑定字典?

时间:2016-11-07 02:55:22

标签: asp.net-mvc asp.net-web-api asp.net-core

我需要处理http://host.my/path?normal=1&othernormal=2&dict_a=b&dict_x=y

这是我最近的尝试,基于How to use Bind Prefix?

public string Get(int normal, int othernormal,
    [Bind(Prefix="dict_")]
    Dictionary<string, string> dict) { ... }

这会创建一个字典,但它是空的。

请务必注意,事先并不知道dict_adict_x。例如。必须允许dict_abracadabra=bla

1 个答案:

答案 0 :(得分:0)

您可以将这些值绑定到没有绑定前缀的字典。字典将使用查询字符串中的任何简单值。

您的示例查询和方法签名(没有绑定前缀),它似乎按预期工作 debugger

我在MVC 1.1.0-preview1-final中测试了这个。

来自此处的文档:https://docs.microsoft.com/en-us/aspnet/core/mvc/models/model-binding