Search code examples
azureazure-functions

How to make a route parameter optional in Azure Function


How to make a route parameter optional in Azure Function

public static async Task<HttpResponseMessage> Run(
    [HttpTrigger(AuthorizationLevel.Function, "get", "post",
        Route = "ResolveKey/{key}/{resolver}")]
    HttpRequestMessage req,
    TraceWriter log,
    string key,
    string resolver = "default")

In the above code I tried to make resolver parameter optional by setting a default value string resolver = "default" . The code compiles and runs fine, but the URL always wants resolver parameter to be present, otherwise I get 404.

I want to make the resolver parameter optional in the above code. Is there any way?


Solution

  • You can express that a parameter is optional in the route template itself.

    For the route above, you can just change your template to the following:

    ResolveKey/{key}/{resolver?}
    

    You can find more information about optional routes and default values here