Admiral Land
Admiral Land

Reputation: 2492

How to make controller with changings parameters?

I have request like this:

GET: /accounts/filter/?status_neq=test&birth_lt=643972596&country_eq=Endgland&limit=5&query_id=110

So, the next request coud be like this:

GET: /accounts/filter/?status_eq=test&birth_gt=643972596&country_year=1970&limit=5&query_id=110

And so on.

I create simple controller, but i do not know how to do this so that you accept such variable input parameters in the controller:

[ResponseCache(CacheProfileName = "FilterCache", Duration = 10, Location = ResponseCacheLocation.Any, NoStore = false)]
[Route("/accounts/filter/")]
[ApiController]
public class FilterController : ControllerBase
{
    // GET: api/Filter
    [HttpGet]
    public IEnumerable<string> Get()
    {
        return new string[] { "value1", "value2" };
    }

    // GET: api/Filter/5
    [HttpGet("{id}", Name = "Get")]
    public string Get(int id)
    {
        return "value";
    }

    // POST: api/Filter
    [HttpPost]
    public void Post([FromBody] string value)
    {
    }

    // PUT: api/Filter/5
    [HttpPut("{id}")]
    public void Put(int id, [FromBody] string value)
    {
    }

    // DELETE: api/ApiWithActions/5
    [HttpDelete("{id}")]
    public void Delete(int id)
    {
    }
}

Upvotes: 0

Views: 100

Answers (2)

Edward
Edward

Reputation: 29996

In general, you should define different parameters for different query string.

If you do not want to specify the parameters in controller action, you could try HttpContext.Request.Query to get all query string.

public ActionResult MultipleParameters()
{
    var parameters = HttpContext.Request.Query;
    return Ok(parameters.ToList());
}

Upvotes: 1

Kunal Mukherjee
Kunal Mukherjee

Reputation: 5853

You need to pass it as a list of parameters to your action method inside the controller or you can also use the FromQuery attribute.

[ResponseCache(CacheProfileName = "FilterCache", Duration = 10, Location = ResponseCacheLocation.Any, NoStore = false)]
[Route("/accounts/filter/")]
[ApiController]
public class FilterController : ControllerBase
{
    // GET: /accounts/filter/?status_eq=test&birth_gt=643972596&country_year=1970&limit=5&query_id=110
    [HttpGet]
    public IEnumerable<string> Get(string status_eq, long birth_gt, int country_year, int limit, int query_id)
)
    {
        return new string[] { "value1", "value2" };
    }
}

or

[ResponseCache(CacheProfileName = "FilterCache", Duration = 10, Location = ResponseCacheLocation.Any, NoStore = false)]
[Route("/accounts/filter/")]
[ApiController]
public class FilterController : ControllerBase
{
    // GET: /accounts/filter/?status_eq=test&birth_gt=643972596&country_year=1970&limit=5&query_id=110
    [HttpGet]
    public IEnumerable<string> Get([FromQuery] string status_eq, [FromQuery] long birth_gt, [FromQuery] int country_year, [FromQuery] int limit, [FromQuery] int query_id)
)
    {
        return new string[] { "value1", "value2" };
    }
}

Upvotes: 2

Related Questions