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?
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
Azure Functions now support regular expressions. You can change your routing template to
ResolveKey/{key}/{*resolver}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With