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:

[FunctionName("WildcardExample")]
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.

@briandunnington


2020.11.25

Serializing JObject to camelCase

How to serialize a JSON.NET JObject to camelCase

View details »


2020.03.20

Azure Functions: Trigger Attribute Settings From Key Vault

How to read trigger attribute settings from Key Vault

View details »


2020.03.19

Azure Functions: Wildcard Routing

A quick tip to support wildcard routes in Azure Functions

View details »


2019.08.01

Azure Functions: Route Priority

Making Azure Functions route matching make more sense

View details »


2019.07.30

Azure Functions: Set Cosmos DB Trigger Connection String At Runtime

How to specify the trigger connection string at runtime and avoid storing secrets during local development

View details »


More Posts >