Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to access query parameters like $orderBy in controller actions?

I am just reading the Microsoft REST API Guidlines (https://github.com/Microsoft/api-guidelines/blob/master/Guidelines.md) and there are query parameters described beginning with a dollar sign, e.g. $orderBy.

9.6 Sorting collections

The results of a collection query MAY be sorted based on property values. The property is determined by the value of the $orderBy query parameter.

Now if I try to define a method parameter like $orderBy in an action method then it is not syntactically correct ($orderBy is not a valid identifier).

public class ExampleController : Controller
{
    // this is syntactically not correct
    public IActionResult Collection(...., string $orderBy = null)
    {
         ...
    }
}

How can I access a query parameter beginning with a dollar sign in an action method of ASP.NET Core ?

like image 572
Ralf Bönning Avatar asked Aug 24 '16 09:08

Ralf Bönning


People also ask

What are query parameters in Servicenow?

The query parameters are specific to the selected API method. A default set of query parameters are displayed for the API. To add additional query parameters, use the Add query parameter button to add a new parameter to the query.


1 Answers

Use FromQuery and set name [FromQuery(Name = "$orderBy")]string orderBy:

public class ExampleController : Controller
{        
    public IActionResult Collection(...., [FromQuery(Name = "$orderBy")]string orderBy = null)
    {
         ...
    }
}
like image 105
adem caglin Avatar answered Sep 17 '22 07:09

adem caglin