处理 Azure 函数表绑定中的可选参数

问题描述

我正在尝试对 Azure 函数使用过滤器,以根据可选路由参数在表中查找行。如果未提供参数,或与行不匹配,则应返回认行。如果我提供匹配的值或错误的值,这会起作用,我会按预期获得一行。如果我根本不提供任何值,则会出现以下错误

Exception while executing function: Functions.link. Microsoft.Azure.WebJobs.Extensions.Storage: Object reference not set to an instance of an object.

三个关键位 (AFAIU) 是:

  • httpTrigger 路由"link/{ref?}"
  • 表格过滤器"Email eq '{ref}' or Default eq true"
  • 端点网址https://[subdomain].azurewebsites.net/api/link/(ref)

有什么方法可以构造过滤器或路由,以便在未提供可选参数时获得过滤器的第二个子句?或者有没有更好的方法来做到这一点?

我的完整 function.json 如下所示:

{
  "bindings": [
    {
      "authLevel": "anonymous","type": "httpTrigger","route": "link/{ref?}","direction": "in","name": "req","methods": [
        "get"
      ]
    },{
      "name": "emailRule","type": "table","take": "1","filter": "Email eq '{ref}' or Default eq true","tableName": "RedirectRules","connection": "TestStorageConnectionAppSetting","direction": "in"
    },{
      "type": "http","direction": "out","name": "res"
    }
  ]
}

解决方法

尝试使用此路由 "route": "link/{ref=''}", 这样您就没有空值但始终为空字符串