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?

10
  • 1
    why the %24 ? Commented Jul 25, 2018 at 8:45
  • 1
    Are you trying to create an oData like url schema? Commented Jul 25, 2018 at 8:46
  • 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? Commented Jul 25, 2018 at 8:54
  • 1
    Ok, good luck: I did it once myself... it was quite a struggle since the documentation is a bit... minimalistic. Commented Jul 25, 2018 at 9:02
  • 1
    Here's some stuff about the subject as well: github.com/OData/WebApi/issues/1177#issuecomment-358659774 Commented Jul 25, 2018 at 9:09

2 Answers 2

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

Sign up to request clarification or add additional context in comments.

Comments

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.