Azure Functions: Wildcard Routing

Azure Functions: Wildcard Routing

A quick tip to support wildcard routes in Azure Functions

I have been using and writing about Azure Functions quite a bit lately, including:

Today's tip is a quick and easy one. Some times you want to handle a bunch of routes with a single function. Maybe you are building a pass-thru to call another API. You might be able to use Azure Functions Proxies, but if you need to execute custom code, that option is out. What you need is a way to set up a wildcard route in your HTTP trigger.

The docs say "You can use any Web API Route Constraint with your parameters". [Side note: There are lots of cool things you can do with route parameters like using regular expressions, optional parameters, and default values - check them all out]. But one thing you won't see documented there is anything about wildcard parameters.

But it turns out that they are supported if you know the syntax. And the syntax is the same one that is used by Proxies: /api/{*restOfPath}. The docs say: "If the route template terminates in a wildcard, such as /api/{*restOfPath}, the value {restOfPath} is a string representation of the remaining path segments from the incoming request."

So you can use this same technique in your HTTP trigger binding set up:

public async Task WildcardExample([HttpTrigger(AuthorizationLevel.Anonymous, "get", 
                                    Route = "{*restOfPath}")]) string restOfPath)
    // use restOfPath in your function here

I saw a few questions about this on the internet so I thought I would share this simple solution.