How to bind a Dictionary from URL query, based on

2019-09-13 08:03发布

I need to handle http://host.my/path?normal=1&othernormal=2&dict_a=b&dict_x=y

Here's my latest attempt at it, based off How to use Bind Prefix? :

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

That creates a dictionary, however, it is empty.

It is important to note, that dict_a, and dict_x are not known in advance. E.g. dict_abracadabra=bla must be allowed.

1条回答
虎瘦雄心在
2楼-- · 2019-09-13 08:52

You're able to bind these values to a dictionary without a bind prefix. A dictionary will consume any simple values in the querystring.

Your sample query and method signature (without the bind prefix), and it seems to work pretty much as expected: debugger

I've tested this in MVC 1.1.0-preview1-final.

From the docs here: https://docs.microsoft.com/en-us/aspnet/core/mvc/models/model-binding

查看更多
登录 后发表回答