How to read values from the querystring with ASP.NET Core?

I’m building one RESTful API using ASP.NET Core MVC and I want to use querystring parameters to specify filtering and paging on a resource that returns a collection.

In that case, I need to read the values passed in the querystring to filter and select the results to return.

I’ve already found out that inside the controller Get action accessing HttpContext.Request.Query returns one IQueryCollection.

The problem is that I don’t know how it is used to retrieve the values. In truth, I thought the way to do was by using, for example

string page = HttpContext.Request.Query["page"]

The problem is that HttpContext.Request.Query["page"] doesn’t return a string, but a StringValues.

Anyway, how does one use the IQueryCollection to actually read the querystring values?

13 Answers
13

Leave a Comment