2

is there a way to set optional query parameters in Azure Functions? The parameter should not be set as route parameter. To get the query parameters i use following code snipped

IDictionary<string, string> queryParams = req.GetQueryParameterDictionary();

Methode signature is following:

public async Task<IActionResult> Function(
        [HttpTrigger(AuthorizationLevel.Function, "post", Route = null)] HttpRequest req,
        [DurableClient] IDurableOrchestrationClient starter
        )
korates
  • 35
  • 1
  • 4

1 Answers1

2

If you don't want to set it as the route parameter, you can use like below:

string param = req.Query["param"];

if (string.IsNullOrEmpty(param)) { 
                //do nothing.
            } else { 
                //do something.
            }
halfer
  • 19,824
  • 17
  • 99
  • 186
Cindy Pau
  • 13,085
  • 1
  • 15
  • 27
  • hi Zhu, doesn't this cause an exception before you get into the "if"? – korates Aug 13 '20 at 08:26
  • @korates Please have a try.:) It will not cause exception. If it dont get specific param, then varible param will be null.(You can debug on your side.) – Cindy Pau Aug 13 '20 at 08:47
  • This implementation led to following exception: at System.ThrowHelper.ThrowKeyNotFoundException[T](T key) at System.Collections.Generic.Dictionary`2.get_Item(TKey key) at – korates Aug 13 '20 at 14:34
  • the solution was to check the input whetever it exists or not. See folowing link: https://stackoverflow.com/questions/2829873/how-can-i-detect-if-this-dictionary-key-exists-in-c Thank you for your answer. – korates Aug 14 '20 at 08:11