Using ASP.NET Core 1.1 with VS2015 (sdk 1.0.0-preview2-003131), I have the following controller:
public class QueryParameters
{
public int A { get; set;
You can consider using the model binding feature of the framework
According to documentation here: Customize model binding behavior with attributes
MVC contains several attributes that you can use to direct its default model binding behavior to a different source. For example, you can specify whether binding is required for a property, or if it should never happen at all by using the
[BindRequired]or[BindNever]attributes.
So I suggest you add a BindRequiredAttribute to the model property.
public class QueryParameters
{
[BindRequired]
public int A { get; set; }
public int B { get; set; }
}
From there the framework should be able to handle the binding and updating model state so that you can check the state of the model in the action
[Route("api/[controller]")]
public class ValuesController : Controller
{
// GET api/values
[HttpGet]
public IActionResult Get([FromQuery]QueryParameters parameters)
{
if (ModelState.IsValid)
{
return Ok(new [] { parameters.A.ToString(), parameters.B.ToString() });
}
return BadRequest();
}
}
The other option would be to create a custom model binder that would fault the action if the required query string is not present.
Reference: Custom Model Binding