Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 932
Ramkumar Singh Avatar asked Jun 16 '17 16:06

Ramkumar Singh


2 Answers

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

like image 73
Fabio Cavalcante Avatar answered Oct 11 '22 11:10

Fabio Cavalcante


Azure Functions now support regular expressions. You can change your routing template to

ResolveKey/{key}/{*resolver}
like image 22
Eelco Koster Avatar answered Oct 11 '22 12:10

Eelco Koster