How to bind a Dictionary from URL query, based on parameter name prefix?

可紊 提交于 2019-12-25 09:19:06

问题


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:


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:

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



来源:https://stackoverflow.com/questions/40457149/how-to-bind-a-dictionary-from-url-query-based-on-parameter-name-prefix

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!