【问题标题】:How to use multiple optional parameter in Azure function如何在 Azure 函数中使用多个可选参数
【发布时间】:2019-12-05 18:59:05
【问题描述】:

如何在 Azure 函数中使用多个可选参数? 我正在使用一个参数创建这样的 Azure 函数,它正在工作。

public async Task<IActionResult> GetRName(
            [HttpTrigger(AuthorizationLevel.Function, "get",  
            Route = "CommonResource/{subscriptionId?}")] HttpRequest req,
            string subscriptionId, 
            string resourcetype, 
            string location, 
            ILogger log)
        {
         ---
        }

如果我添加这样的附加参数会出错。

[FunctionName("GetResourceName")]
        public async Task<IActionResult> GetRName(
            [HttpTrigger(AuthorizationLevel.Function, "get",  Route = "CommonResource/{subscriptionId:string?}/{resourcetype?}")] HttpRequest req,
            string subscriptionId ,string resourcetype , ILogger log)
        {
            log.LogInformation("This API should return Resource name.");

            //string resourcetype = req.Query["resourcetype"];
            resourcetype = req.Query["resourcetype"];
            string location = req.Query["location"];

请查看随附的屏幕截图以供参考。

【问题讨论】:

  • 你要使用什么参数?
  • 我想使用资源类型和位置。有可能吗?

标签: azure-functions asp.net-core-webapi azure-function-async


【解决方案1】:

路由“api/CommonResource/{subscriptionId:string?}/{resourcetype?}”上的约束条目“subscriptionId”-“string”无法被“DefaultInlineConstraintResolver”类型的约束解析器解析。

您使用的是不支持的{subscriptionId:string?},导致上述错误。

This document列出了支持的约束,请参考。

【讨论】:

    【解决方案2】:

    你需要改变你的 Route 属性

    Route = "CommonResource/{subscriptionId:int}/{resourcetype}")
    

    如果你想使用多个可选参数,只有连续的参数才有效

    Route = "CommonResource/{subscriptionId:int?}/{resourcetype?}")
    

    【讨论】:

    • 添加此代码时出现错误 Route = "CommonResource/{subscriptionId:int?}/{resourcetype?}")
    猜你喜欢
    • 1970-01-01
    • 2013-07-17
    • 2018-03-25
    • 1970-01-01
    • 2015-04-27
    • 1970-01-01
    • 2019-01-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多