API – Functions Part 3: Routing

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: Options

Note, this material is based on our book Programming APIs with C# and .NET from Packt by Jesse Liberty and Joseph Deluzen.

Unknown's avatar

About Jesse Liberty

Jesse Liberty has three decades of experience writing and delivering software projects and is the author of 2 dozen books and a couple dozen online courses. His latest book, Building APIs with .NET, is now available wherever you buy your books. Liberty is a Senior SW Engineer for CNH and he was a Senior Technical Evangelist for Microsoft, a Distinguished Software Engineer for AT&T, a VP for Information Services for Citibank and a Software Architect for PBS. He is a Microsoft MVP.
This entry was posted in API, APIs, Essentials, Mini-Tutorial and tagged . Bookmark the permalink.

671 Responses to API – Functions Part 3: Routing

  1. Hi my friend! I want to say that this article is amazing, great written and come with almost all significant infos. I?¦d like to see extra posts like this .

  2. filma24's avatar filma24 says:

    Hi, Neat post. There is a problem with your web site in internet explorer, would check this… IE still is the market leader and a big portion of people will miss your fantastic writing due to this problem.

  3. atm4d's avatar atm4d says:

    Thanks for the recommendations shared on your own blog. Another thing I would like to express is that losing weight is not supposed to be about going on a dietary fads and trying to get rid of as much weight as possible in a couple of weeks. The most effective way in losing weight is by acquiring it slowly and gradually and obeying some basic suggestions which can help you to make the most from a attempt to lose weight. You may understand and already be following many of these tips, but reinforcing understanding never does any damage.

  4. Thanks – Enjoyed this blog post, is there any way I can receive an update sent in an email every time you write a fresh update?

  5. toto macau's avatar toto macau says:

    Hey, I think your blog might be having browser compatibility issues. When I look at your blog site in Chrome, it looks fine but when opening in Internet Explorer, it has some overlapping. I just wanted to give you a quick heads up! Other then that, awesome blog!

  6. slot online's avatar slot online says:

    Thanks a lot for sharing this with all of us you actually know what you’re talking about! Bookmarked. Please also visit my website =). We could have a link exchange contract between us!

  7. Very interesting details you have noted, thankyou for putting up. “My work is a game, a very serious game.” by M. C. Escher.

Leave a Reply

Your email address will not be published. Required fields are marked *