Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.NET core FromQuery getting invalid parameters with dot sign inside

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

like image 917
Jacob Sobus Avatar asked Sep 01 '17 09:09

Jacob Sobus


People also ask

What is the difference between FromRoute and FromQuery?

[FromQuery] - Gets values from the query string. [FromRoute] - Gets values from route data.

How do I make query string mandatory?

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.

What does FromBody mean in C#?

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.


1 Answers

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);
like image 94
Mathieu Renda Avatar answered Oct 26 '22 23:10

Mathieu Renda