Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Controller's action with dictionary as parameter stripping to dot

I have an Action that receives a class with a dictionary in its properties:

public ActionResult TestAction(TestClass testClass)
{
    return View();
}

public class TestClass
{
    public Dictionary<string, string> KeyValues { get; set; }
}

If I do a post to my action with the following JSON:

{
  "KeyValues": {
    "test.withDoT": "testWithDot"
  }
}

The key in my dictionary is stripped to the dot and has nothing in the value.

enter image description here

Trying without the dot works. How can I do a post with a dot in a Dictionary<string, string> with MVC?

like image 364
Alexandre Pepin Avatar asked Jul 22 '15 13:07

Alexandre Pepin


1 Answers

We gave a blind try supposing that there is a regex parser somewhere in the deep (well it was minimal the chance) and to escape 'dot'.

After thinking a while I concluded: dot is not a legal char in identifiers. Yes I know it is a key in the C# dictionary, but in the json part (and javascript) it could be in the identifier syntax role.

So I strongly suggest to replace client side the . (dot) with an escape sequence like _dot_ and replace back it in server side. Performance will suffer of course.

like image 193
g.pickardou Avatar answered Sep 28 '22 17:09

g.pickardou