Skip to content

Instantly share code, notes, and snippets.

@kierenj
Created March 1, 2018 14:30
Show Gist options
  • Save kierenj/1a38e03b2668669e9d41fc44cb93f7e4 to your computer and use it in GitHub Desktop.
Save kierenj/1a38e03b2668669e9d41fc44cb93f7e4 to your computer and use it in GitHub Desktop.
[HttpGet]
[Route("products")]
public IHttpActionResult GetProducts([FromUri]string filter, [FromHeader]StandardGetHeaders headers)
{
// validate headers (and any other bound fields), returning useful messages
// upon failure
if (!ModelState.IsValid) return BadRequest(ModelState);
// run our query
var results = RunGetProductsQuery(
filter,
headers.XPageNumber,
headers.XPageSize);
return Json(results);
}
// …
// this is a supporting “header model” class – to receive header values (if present)
// and define validation rules in a standardized way
public class StandardGetHeaders
{
[Required("Wenk!")]
public string XSecretPenguinHandshake { get; set; }
[Range(1, int.MaxValue, "Page number must be greater than 0")]
public int XPageNumber { get; set; }
[Range(1, 100, "Page size must be between 1 and 100")]
public int XPageSize { get; set; }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment