Created a new Azure Function in Visual Studio 2017 (HTTPTrigger
based) and having difficulty passing parameters with custom route. Below is the code excerpt:
[FunctionName("RunTest")]
public static async Task<HttpResponseMessage> Run(
[HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = "orchestrators/contoso_function01/{id:int}/{username:alpha}")] HttpRequestMessage req, TraceWriter log)
{
log.Info("C# HTTP trigger function processed a request.");
// parse query parameter
string name = req.GetQueryNameValuePairs()
.FirstOrDefault(q => string.Compare(q.Key, "id", true) == 0)
.Value;
string instanceId = req.GetQueryNameValuePairs()
.FirstOrDefault(q => string.Compare(q.Key, "username", true) == 0)
.Value;
if (name == null)
{
// Get request body
dynamic data = await req.Content.ReadAsAsync<object>();
name = data?.name;
}
return name == null
? req.CreateResponse(HttpStatusCode.BadRequest, "Please pass a name on the query string or in the request body")
: req.CreateResponse(HttpStatusCode.OK, "Hello " + name);
}
I tried to access the Function with the below URLs, but couldn't retrieve the ID
or UserName
values from the query string using the GetQueryNameValuePairs()
API, as it has only 0 items in the collection:
http://localhost:7071/api/orchestrators/contoso_function01/123/abc
http://localhost:7071/api/orchestrators/contoso_function01/?id=123&username=abc