7

I have a .Net Core 2.2 Web API. One of my controller action methods takes two query string parameter, a year and a month.

GET: /api/ItemsForMonth?year=2019&month=8

The action method looks like this:

[HttpGet]
public async Task<ActionResult<IEnumerable<Item>>> GetItemsForMonth([FromQuery] int year, [FromQuery] int month)
{
    if (year <= 2000 || month <= 0 || month > 13)
        return BadRequest("Please check the year and month parameters.");
}

So I am checking to make sure that the year is greater than 2000, and the month is between 1 and 12.

Is that the best way of doing it? I know if the parameters were part of the route instead of query strings (and maybe they should be?) I could do this

GET: /api/ItemsForMonth/2019/8

[HttpGet("/{year:int:min(2000)}/{month:int:min(1):max(12)}")]
public async Task<ActionResult<IEnumerable<Item>>> GetItemsForMonth()
{
}

But is there something similar for query string parameters?

Thanks

2 Answers 2

16

One approach would be to bind the query parameters into a model and use basic attribute-based model validation:

class DateQueryParameters {
  [Required]
  [Range(2000, int.MaxValue)]
  public int Year {get;set;}

  [Required]
  [Range(1, 12)]
  public int Month {get;set;}
}

[HttpGet]
public async Task<IActionResult> GetItemsForMonth([FromQuery] DateQueryParameters dateParameters)
{
    if(!this.ModelState.IsValid){
       return Task.FromResult(this.BadRequest(this.ModelState));
    }
}

Model validation will be done automatically if your controller is decorated with the ApiController attribute.

Sign up to request clarification or add additional context in comments.

3 Comments

Excellent suggestion. Thanks Jota.
Hi @Jota.Toledo, how can I disable the automatic validation of ApiController? I post an invalid object to my endpoint but the thing is the automatic validation has done before the code can go to my method.
Unfortunately net core 6 minimal API doesn't provide automatic mapping of querystring data to a complex object. This is something I'm missing a lot
1

yes, now there is. Refer to the following docs page: https://learn.microsoft.com/en-us/aspnet/web-api/overview/web-api-routing-and-actions/attribute-routing-in-web-api-2#route-constraints

here's the sample code:

[HttpGet("/{year:int:min(2000)}/{month:int:min(1):max(12)}/{query:maxlength(15)}")]
public async Task<ActionResult<IEnumerable<Item>>> GetItemsForMonth()
{
}

notice the "/{query:maxlength(15)}" in the end of the route params list.

2 Comments

Those are not query string parameters. The question was about them.
Here, year and month are still part of url and not query string.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.