Problem
I’m using ASP.NET Core MVC to create a RESTful API, and I’d like to use querystring parameters to provide filtering and paging on a resource that returns a collection.
In that situation, I’ll need to read the querystring values to filter and select the results I want to return.
I’ve already discovered that the Get action in the controller uses HttpContext. Request. IQueryCollection is returned by query.
The issue is that I’m not sure how it’s used to get the values. To be honest, I thought the best way to go about it was to use, say,
string page = HttpContext.Request.Query["page"]
The issue is that HttpContext.Request.Query[“page”] returns a StringValues rather than a string.
Anyway, how does one really read the querystring data using the IQueryCollection?
Asked by user1620696
Solution #1
To bind a specific model to the querystring, use [FromQuery]:
e.g.
[HttpGet()]
public IActionResult Get([FromQuery(Name = "page")] string page)
{...}
Answered by Mike_G
Solution #2
If only a single page parameter is given, the ToString function on IQueryCollection will return the desired value:
string page = HttpContext.Request.Query["page"].ToString();
If there are multiple values, such as?page=1&page=2, the ToString method will return 1,2.
But as @mike-g suggested in his answer you would better use model binding and not directly accessing the HttpContext. Object Request.Query
Answered by Darin Dimitrov
Solution #3
By default, ASP.NET Core binds form values, route values, and query strings by name. This basically means that you can perform the following:
[HttpGet()]
public IActionResult Get(int page)
{ ... }
Model Binding in ASP.NET Core is the source of this information.
It’s worth noting that you can combine the automatic and explicit methods:
[HttpGet()]
public IActionResult Get(int page
, [FromQuery(Name = "page-size")] int pageSize)
{ ... }
Answered by spottedmahn
Solution #4
Here’s an example of code I used (with a.NET Core view):
@{
Microsoft.Extensions.Primitives.StringValues queryVal;
if (Context.Request.Query.TryGetValue("yourKey", out queryVal) &&
queryVal.FirstOrDefault() == "yourValue")
{
}
}
Answered by Pavel
Solution #5
You may just make an object that looks like this:
public class SomeQuery
{
public string SomeParameter { get; set; }
public int? SomeParameter2 { get; set; }
}
Then just make something like this in controller:
[HttpGet]
public IActionResult FindSomething([FromQuery] SomeQuery query)
{
// Your implementation goes here..
}
Even better, you can build an API model from the following:
[HttpGet]
public IActionResult GetSomething([FromRoute] int someId, [FromQuery] SomeQuery query)
to:
[HttpGet]
public IActionResult GetSomething(ApiModel model)
public class ApiModel
{
[FromRoute]
public int SomeId { get; set; }
[FromQuery]
public string SomeParameter { get; set; }
[FromQuery]
public int? SomeParameter2 { get; set; }
}
Answered by Mariusz
Post is based on https://stackoverflow.com/questions/41577376/how-to-read-values-from-the-querystring-with-asp-net-core