Azure Functions 中是否可以进行属性路由 [英] Is Attribute Routing possible in Azure Functions

查看:12
本文介绍了Azure Functions 中是否可以进行属性路由的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将路由参数强制为 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 Functions 中是否可以进行属性路由的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆