How to read values from the querystring with ASP.N

2020-01-26 04:46发布

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?

10条回答
放荡不羁爱自由
2楼-- · 2020-01-26 05:05

You can just create an object like this:

public class SomeQuery
{
    public string SomeParameter { get; set; }
    public int? SomeParameter2 { get; set; }
}

And then in controller just make something like that:

[HttpGet]
public IActionResult FindSomething([FromQuery] SomeQuery query)
{
    // Your implementation goes here..
}

Even better, you can create API model from:

[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; }
}
查看更多
趁早两清
3楼-- · 2020-01-26 05:06

IQueryCollection has a TryGetValue() on it that returns a value with the given key. So, if you had a query parameter called someInt, you could use it like so:

var queryString = httpContext.Request.Query;
StringValues someInt;
queryString.TryGetValue("someInt", out someInt);
var daRealInt = int.Parse(someInt);

Notice that unless you have multiple parameters of the same name, the StringValues type is not an issue.

查看更多
The star\"
4楼-- · 2020-01-26 05:09

You can use [FromQuery] to bind a particular model to the querystring:

https://docs.microsoft.com/en-us/aspnet/core/mvc/models/model-binding

e.g.

[HttpGet()]
public IActionResult Get([FromQuery(Name = "page")] string page)
{...}
查看更多
▲ chillily
5楼-- · 2020-01-26 05:16

ASP.NET Core will automatically bind form values, route values and query strings by name. This means you can simply do this:

[HttpGet()]
public IActionResult Get(int page)
{ ... }

MVC will try to bind request data to the action parameters by name ... below is a list of the data sources in the order that model binding looks through them

  1. Form values: These are form values that go in the HTTP request using the POST method. (including jQuery POST requests).

  2. Route values: The set of route values provided by Routing

  3. Query strings: The query string part of the URI.

Source: How model binding works


FYI, you can also combine the automatic and explicit approaches:

[HttpGet()]
public IActionResult Get(int page
     , [FromQuery(Name = "page-size")] int pageSize)
{ ... }
查看更多
登录 后发表回答