I'm trying to retrieve an int
value from my URI's querystring
but I've encountered some problems.
I have a request URI that is as follows
http://localhost:64813/api/MyController/GetTree?%24filter=parentId%20eq%207
I send it via postman, and controller receives it, but I can't access parentId
's value. What am I doing wrong?
Here's what I tried so far on my controller... none of these tries is working though…
1) I tried to retrieve a string
named filter, but it was null
...
[AllowAnonymous]
[HttpGet("GetTree")]
public IActionResult GetTree(string filter)
{
int q = filter.LastIndexOf(" eq ");
string r = parentId.Substring(q);
int result = Convert.ToInt32(r);
2) I tried to use [FromQuery]
to access parentId
from filter
, but nothing. Always null
value.
[AllowAnonymous]
[HttpGet("GetTree")]
public IActionResult GetTree([FromQuery(Name = "filter")] string parentId)
{
int q = parentId.LastIndexOf(" eq ");
string r = parentId.Substring(q);
int result = Convert.ToInt32(r);
3) Like second try, but I tried to get the whole query instead. As you can guess, the string
was null
, again...
[AllowAnonymous]
[HttpGet("GetTree")]
public IActionResult GetNodi([FromQuery] string filter)
{
int q = filter.LastIndexOf(" eq ");
string r = filter.Substring(q);
int result = Convert.ToInt32(r);
Other notable tries:
4) Did as try #2, but I tried to parse parentId
as an int
instead of string
. Nothing, it was always zero as default value, even if i changed the request value.
5) Tried by parsing filter
as an object (see below) with try #1, try #2 and #3. The string was always null
.
public class NodeReq
{
public string ParentId { get; set; }
}
What have I done wrong? How can I access this querystring parentId
value?