7

I am trying to send some parameters with GET request to the controller in query string. I am getting all parameters that are inside query string to one Dictionary<string, string> using [FromQuery]. It is working well until one of the parameter name contains dot (.) sign. I checked Request.Query object and it looks that it's parsed well but my Dictionary object get this one, exact item wrong. So it looks like bug in [FromQuery] binder or i am doing something wrong. Code with debbuged values of Request.Query and mine parameters below:enter image description here

This is how the query string that is sent looks like: ?query=&InspectionType=Safety&ItemType=InspectionPoint&RecordParentGUID=9275bee2-0a2d-461c-8835-51880e76f035&parent.ResultClassCode=parent.ResultClassCode

UPDATE: Got answer from Eilon Lipton working in .net developers team, in short - this is by design. Dot sign . and [, ] are special ones used for denoting properties and indexers. Full answer available here: https://github.com/aspnet/Mvc/issues/6746

Jacob Sobus
  • 961
  • 16
  • 25
  • 1
    Indeed, you should file a bug report in http://github.com/aspnet/mvc. You have a look at the code for DictionaryModelBinderProvider, this is the class responsible for binding to a dictionary. – Ricardo Peres Sep 01 '17 at 10:21
  • @MichalHainc I found that dot sign is unreserved characters so don't need to encode them: https://stackoverflow.com/questions/1455578/characters-allowed-in-get-parameter – Jacob Sobus Sep 01 '17 at 10:43
  • One thing worth noting is that the GUID is picking up correct value. only the other ones (probably Enums) are not picking any values. event the last one which is a string, picks up the value – Gurpreet Sep 01 '17 at 12:24
  • @Gurpreet all of this parameters are strings, they come from ajax call in js. I used Dictionary not to bind it to any other type but as you see still when a key have a dot sign - it breaks. – Jacob Sobus Sep 05 '17 at 07:00

2 Answers2

7

The . (dot), [ and ] characters are used everywhere internally by ASP.NET MVC Core as path separators in the model binding process and there is (AFAIK) no way around that.

If you want to access the raw values from the query string, Request.Query is by far the simplest solution.

To turn it into a pure Dictionary<string, string>:

Request.Query.ToDictionary(kvp => kvp.Key, kvp => kvp.Value);
Mathieu Renda
  • 14,069
  • 2
  • 35
  • 33
1

Can you use a partial view to flatten parent.ResultClassCode to ResultClassCode?

The partial could look like this

@model parent
<div class="form-group">
    <label asp-for="ResultClassCode"></label><span asp-validation-for="ResultClassCode" class="text-danger"></span>
    <input asp-for="ResultClassCode" class="form-control">
</div>
Lee Reitz
  • 105
  • 10