How to use multiple optional parameter in Azure function

2.4k Views Asked by At

How to use multiple optional parameter in Azure function? I am creating Azure function like this with one parameter, it is working.

public async Task<IActionResult> GetRName(
            [HttpTrigger(AuthorizationLevel.Function, "get",  
            Route = "CommonResource/{subscriptionId?}")] HttpRequest req,
            string subscriptionId, 
            string resourcetype, 
            string location, 
            ILogger log)
        {
         ---
        }

If i add additional parameter like this getting an error.

[FunctionName("GetResourceName")]
        public async Task<IActionResult> GetRName(
            [HttpTrigger(AuthorizationLevel.Function, "get",  Route = "CommonResource/{subscriptionId:string?}/{resourcetype?}")] HttpRequest req,
            string subscriptionId ,string resourcetype , ILogger log)
        {
            log.LogInformation("This API should return Resource name.");

            //string resourcetype = req.Query["resourcetype"];
            resourcetype = req.Query["resourcetype"];
            string location = req.Query["location"];

Please check the attached screen shot for your referance.

enter image description here

2

There are 2 best solutions below

1
On

You need to change you Route property

Route = "CommonResource/{subscriptionId:int}/{resourcetype}")

If you want to use multiple optional parameters, only consecutive parameters will work

Route = "CommonResource/{subscriptionId:int?}/{resourcetype?}")
0
On

The constraint entry 'subscriptionId' - 'string' on the route 'api/CommonResource/{subscriptionId:string?}/{resourcetype?}' could not be resolved by the constraint resolver of type 'DefaultInlineConstraintResolver'.

You are using {subscriptionId:string?} that is not supported, which cause the above error.

This document lists the supported constraints, please refer to it.

enter image description here