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:
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
[FromQuery] - Gets values from the query string. [FromRoute] - Gets values from route data.
The first solution to make query string parameters mandatory is to use Model Binding on the public properties of a class. We make the Number property mandatory by using the [BindRequired] attribute.
The [FromBody] attribute which inherits ParameterBindingAttribute class is used to populate a parameter and its properties from the body of an HTTP request. The ASP.NET runtime delegates the responsibility of reading the body to an input formatter.
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);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With