I have an Azure Function and I want to set a custom HTTP endpoint. Following the answer to this SO question, I ended up with something like this:
[FunctionName("DoSomething")]
public static async Task<IActionResult> Run([HttpTrigger(AuthorizationLevel.Anonymous, "get", Route = "v1/tenants/{tenantId}/locations/{locationId}/products?manufacturer={manufacturer}")]
HttpRequest request, ILogger logger, string tenantId, string locationId, string manufacturer)
{
//
}
However, the route is not accepted by the Webjob:
"v1/tenants/{tenantId}/locations/{locationId}/products?manufacturer={manufacturer}"
The reason is because of the question mark '?':
An error occurred while creating the route with name 'DoSomething' and template 'api/v1/tenants/{tenantId}/locations/{locationId}/products?manufacturer={manufacturer}'. The literal section 'products?manufacturer=' is invalid. Literal sections cannot contain the '?' character. Parameter name: routeTemplate The literal section 'products?manufacturer=' is invalid. Literal sections cannot contain the '?' character.
Question
How can I specify a query parameter in a custom HTTP endpoint of my Azure Function?
Query parameters are a defined set of parameters attached to the end of a url. They are extensions of the URL that are used to help define specific content or actions based on the data being passed. To append query params to the end of a URL, a '? ' Is added followed immediately by a query parameter.
To pass value in the function route in Azure function, we would have to modify the route parameter as “Hello/{valueName}”. Then add a parameter with the same name as the valueName in the Run method to use this value in your azure function. But adding {valueName} makes it a mandatory value to be passed.
Azure Functions may be invoked via HTTP requests to build serverless APIs and respond to webhooks.
The key difference between query parameters and route parameters is that route parameters are essential to determining route, whereas query parameters are optional.
I am afraid it's not possible to put query parameter in Route.
Microsoft.AspNetCore.Routing: The literal section 'products?manufacturer=' is invalid. Literal sections cannot contain the '?' character.
It's a built-in restriction of ASP.NET Routing, which is used by Azure Function to build route for Http trigger.
allow me to get the value as one of the Run's method parameters instead of poking at the HttpRequest instance
If it's the reason why you want to put query parameter in route, I would suggest you add IDictionary<string, string> query
in method signature and use query["manufacturer"]
to access the parameter in function code. But honestly it's almost the same as request.Query["manufacturer"]
.
Or you may have to follow the recommendation, transform the query parameter to route like products/{productId}
.
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