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

** Note ** Jesse is currently looking for a new position. You can learn more about him at https://jesseliberty.bio Thank you. 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 was a Team Lead and Senior Software Engineer for various corporations, 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 13 year Microsoft MVP.
This entry was posted in API, APIs, Essentials, Mini-Tutorial and tagged . Bookmark the permalink.

1,212 Responses to API – Functions Part 3: Routing

  1. bos138 login's avatar bos138 login says:

    When I initially commented I clicked the “Notify me when new comments are added” checkbox and now each time a comment is added I get three emails with the same comment. Is there any way you can remove me from that service? Cheers!

  2. I do agree with all of the concepts you have presented for your post. They’re really convincing and can certainly work. Nonetheless, the posts are too brief for beginners. May just you please prolong them a bit from subsequent time? Thanks for the post.

  3. Great breakdown of routing in APIs, Jesse. I’ve been working on a small project recently and was struggling with how to structure my endpoints cleanly—your examples really clarified the concept for me. Do you have any tips for handling nested routes without making the code too messy?

  4. hello there and thank you for your information – I have definitely picked up something new from right here. I did however expertise some technical issues using this web site, as I experienced to reload the web site many times previous to I could get it to load properly. I had been wondering if your web hosting is OK? Not that I’m complaining, but sluggish loading instances times will often affect your placement in google and can damage your high-quality score if advertising and marketing with Adwords. Anyway I am adding this RSS to my e-mail and could look out for a lot more of your respective interesting content. Make sure you update this again soon..

  5. Edwinveite's avatar Edwinveite says:

    https://pharmmex.today/# farmacia online usa

  6. Louistrugs's avatar Louistrugs says:

    Reliable info. Cheers.
    Wein Plaza – https://weinplaza.com/

  7. Richardkig's avatar Richardkig says:

    aceton apotek: acne plГҐster apotek – linsvГ¤tska apotek

  8. Image Tools's avatar Image Tools says:

    Great breakdown of routing in APIs, Jesse. I’ve been building a small project with Express and your explanation of how route parameters and query strings differ really cleared up some confusion I had about structuring endpoints. Do you have any tips for handling nested routes cleanly without making the code too messy?

  9. dewi88's avatar dewi88 says:

    Really excellent visual appeal on this website , I’d value it 10 10.

Leave a Reply

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