1

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?

Stefan
  • 17,448
  • 11
  • 60
  • 79
Santa Cloud
  • 547
  • 1
  • 10
  • 22
  • 1
    why the `%24` ? – Stefan Jul 25 '18 at 08:45
  • 1
    Are you trying to create an oData like url schema? – Stefan Jul 25 '18 at 08:46
  • @Stefan Actually I'm not pretty sure, since it's a request generated from an angular third-party library (it's named "Devextreme") and sadly I have no control on how it structures the request query… By the way, yes, it uses oData. Here's [an example](https://js.devexpress.com/Demos/WidgetsGallery/Demo/TreeView/VirtualMode/Angular/Light/) – Santa Cloud Jul 25 '18 at 08:52
  • 1
    ... ok, for OData to work for your own routing schema you need to include routing and libraries. It requires a minimal setup. Have you done that? – Stefan Jul 25 '18 at 08:54
  • @Stefan Actually no, I haven't done it yet (also because I didn't know that). I'll give a look to the links provided in your answer and I'll let you know! – Santa Cloud Jul 25 '18 at 09:01
  • 1
    Ok, good luck: I did it once myself... it was quite a struggle since the documentation is a bit... minimalistic. – Stefan Jul 25 '18 at 09:02
  • @Stefan I noticed that… Have you found any example about the Odata controller configuration? I'm trying to find some example on google but nothing... – Santa Cloud Jul 25 '18 at 09:05
  • @Stefan in the meanwhile, I had an idea… What if I simply access the querystring values like `req.QueryString.Value` and I parse them? Could it work the same? – Santa Cloud Jul 25 '18 at 09:06
  • 1
    Here's some stuff about the subject as well: https://github.com/OData/WebApi/issues/1177#issuecomment-358659774 – Stefan Jul 25 '18 at 09:09
  • 1
    It seems that there is a new release: https://www.nuget.org/packages/Microsoft.AspNetCore.OData/7.0.0 – Stefan Jul 25 '18 at 09:12

2 Answers2

3

Your URL is wrong, ith should be:

http://localhost:64813/api/MyController/GetTree?filter=parentId%20eq%207

unless you are using OData, but it doesn't seem so.

If you are using OData, there is no need to escape the characters and it should work with this URL:

http://localhost:64813/api/MyController/GetTree?$filter=parentId eq 7

If you need to setup OData in your .net core project, I would suggest follow this package:

update


Since your angular setup requires an OData like url schema, and I have no control on how it structures the request query; your controllers needs to be compatible with the protocol. More info about this topic can be found here:

https://www.nuget.org/packages/Microsoft.OData.Core/

https://github.com/OData/odata.net

OData Support in ASP.net core

update:


It should be this package:

https://www.nuget.org/packages/Microsoft.AspNetCore.OData/7.0.0

Stefan
  • 17,448
  • 11
  • 60
  • 79
1

Query string key name should match the parameter name filter and in URL its %24filter you will need to removre %24

Your URL should be as below

http://localhost:64813/api/MyController/GetTree?filter=parentId%20eq%207
ElasticCode
  • 7,311
  • 2
  • 34
  • 45