Azure函数中是否可以进行属性路由 [英] Is Attribute Routing possible in Azure Functions
问题描述
我正在尝试将route参数强制为guid
,但低于错误提示
I am trying to enforce a route parameter to be guid
but getting below error
执行函数时发生异常:GetUser->一个或多个错误 发生. ->异常绑定参数'req'->无效的强制转换 "System.String"到"System.Guid"."
"Exception while executing function: GetUser -> One or more errors occurred. -> Exception binding parameter 'req' -> Invalid cast from 'System.String' to 'System.Guid'."
public static async Task<HttpResponseMessage> Run(
[HttpTrigger(AuthorizationLevel.Admin, "get", Route = "GetUser/{userId:guid}")] HttpRequestMessage req,
Guid userId, ILogger log)
{
}
我正在发出的请求是http://localhost:7071/api/GetUser/246fb962-604d-4699-9443-fa3fa840e9eb/
我错过了什么吗?我们不能强制将路线参数设置为Guid吗?
Am i missing some thing? Cannot we enforce route parameter to be guid ?
推荐答案
从"System.String"到"System.Guid"的无效转换
Invalid cast from 'System.String' to 'System.Guid'
当在我这边在Azure httptrigger函数中使用路由约束{userId:guid}
时,我可以重现相同的问题,您可以尝试
I can reproduce same issue when use Route constraint {userId:guid}
in Azure httptrigger function on my side, you can try to open an issue to give a feedback.
此外,如果可能的话,您可以尝试致电 Guid.TryParse方法将字符串转换回功能代码中的Guid值,以下代码供您参考.
Besides, if possible, you can try to call Guid.TryParse method to convert the string back to Guid value in function code, the following code is for your reference.
public static string Run([HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = "GetUser/{userId:guid}")]HttpRequestMessage req, string userId, TraceWriter log)
{
log.Info("C# HTTP trigger function processed a request.");
Guid newGuid;
var resmes = "";
if (Guid.TryParse(userId, out newGuid))
{
resmes = "userid: " + newGuid;
}
else {
resmes = "error";
}
return resmes;
}
这篇关于Azure函数中是否可以进行属性路由的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!