1

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.

Community
  • 1
  • 1
LOST
  • 2,956
  • 3
  • 25
  • 40
  • You would need the url to be `...&dict[0].Key=b&dict[0].Value=y` to bind to your parameter (without the `[Bind(Prefix="dit_")]` Why not just parameters `string dict_a, string dict_x` and create the dictionary from the values? –  Nov 07 '16 at 02:59
  • @StephenMuecke, because I do not control the client. My question actually has the answer to your second question. – LOST Nov 07 '16 at 03:50
  • The you going to need to just read the values from the query string (or create a custom model binder to do it) and create the dictionary from the values. –  Nov 07 '16 at 03:52

1 Answers1

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: debugger

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

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

Neil Cross
  • 502
  • 3
  • 9