In the previous part of this series, we looked at creating our first API. But how do you determine which API is being invoked? This is accomplished with routing.
If you have a Products class your entry (end point) might look like this:
[Function(nameof(Products))] public IActionResult Run(HttpTrigger(AuthorizationLevel.Anonymous, "get", Route="products/{category}/{id: int}")] HttpRequest req, string category, int id) { return new OkObjectResult(new {category, id}); }
Notice the routing property (“products/{category}/{id: int}” — this allows us to customize and restrict the arguments from the desired endpoint. The customer/user can now use the same endpoint for a variety of situations.
You can go further and restrict the type of the parameters, and even provide default values:
Route="products/{category:alpha}/{id: int?}")]
Here we are saying that the category must consist only of standard characters, and the id is optional.
Next up: Deploying to Azure
Note, this material is based on our book Programming APIs with C# and .NET from Packt by Jesse Liberty and Joseph Deluzen.