azure-functions-host
azure-functions-host copied to clipboard
Unable to use "guid" as an HttpTrigger route parameter (Invalid cast from 'System.String' to 'System.Guid')
When trying to pass a guid as a parameter binding within a route definition the following exception is thrown
Exception while executing function: TestParams -> One or more errors occurred. -> Exception binding parameter 'req' -> Invalid cast from 'System.String' to 'System.Guid'.
Investigative information
Please provide the following:
- Timestamp: 2018-06-07T11:27:47.578
- Function App version (1.0 or 2.0-beta): 1.0.12.1
- Function App name: vos6functiondev
- Function name(s) (as appropriate): GetPendingDamages
- Invocation ID: (requestid): 65ead198-8c37-438d-b9a5-ac17165f1e41
- Region: UK West
Repro steps
Provide the steps required to reproduce the problem:
- For an HttpTrigger, configure a route parameter binding for a Guid
[FunctionName("GetPendingDamages")]
public static async Task<HttpResponseMessage> Run(
[HttpTrigger(AuthorizationLevel.Function, "get", Route = "GetPendingDamages/{stayId:guid}")]
HttpRequestMessage req,
Guid stayId,
TraceWriter log)
{...}
- Invoke the fuction as a GET request
- The following exception will be thrown
"message": "Exception while executing function: TestParams -> One or more errors occurred. -> Exception binding parameter 'req' -> Invalid cast from 'System.String' to 'System.Guid'.",
Expected behavior
Provide a description of the expected behavior.
I would expect to either
- have a valid Guid value available as an input parameter
- receive a 400 Bad Request, in case the value could not be parsed as a Guid
Known workarounds
Provide a description of any known workarounds.
You can do a Guid.TryParse() in code, but it would be nice to be able to have the Guid bindings available like in Web API 2
as it has been reported here http://dontcodetired.com/blog/post/Dynamic-Binding-in-Azure-Functions-with-Imperative-Runtime-Bindings and here https://stackoverflow.com/questions/46743180/is-attribute-routing-possible-in-azure-functions
Thanks :)
I've also noticed that the same exception is thrown when binding to a custom POCO in the http request body.
If the POCO contains any Guid params, the binder will fail to parse them as such from the JSON in the payload
We have tracking item here https://github.com/Azure/azure-functions-host/issues/2279 to preserve constraint types in the route. Closing this as dup.
Please reopen this as this is Guid as a parameter in the URL, not a Guid in the poco.
Is there any update to fixing this issue short of using workarounds? The issue is still present with the following configuration.
Visual Studio 2019 (16.0.0) .NET Core 2.2 Azure Functions Core Tools (2.5.553 Commit hash: b63f2d6f5479718ca276f7b585a8cc6b9bc57c4f) Function Runtime Version: 2.0.12382.0
- bump, would like to use guids as query param as well.
I'm going to reopen this because after investigation, it looks like #2279 is unrelated.
@fabiocav reopening for triage. Many users report that Grahm's fix (PR https://github.com/Azure/azure-webjobs-sdk-extensions/pull/586) didn't actually fix this.