Azure函数:如何将http触发器函数的查询字符串参数绑定到Cosmos DB的SQL查询

Car*_*uez 6 azure azure-functions azure-cosmosdb azure-functions-runtime

我正在尝试使用Cosmos DB输入绑定运行http触发器Azure功能.我希望http触发器的url在查询字符串上包含几个参数,这些参数绑定到输入Cosmos DB绑定的SQL查询.我正在尝试以下绑定function.json,但它不起作用(该功能甚至没有被触发):

{
  "direction": "in",
  "type": "httpTrigger",
  "authLevel": "anonymous",
  "name": "req",
  "methods": [ "get" ],
  "route": "users/{age=age?}/{gender=gender?}"
},
{
  "direction": "in",
  "type": "documentDB",
  "name": "users",
  "databaseName": "Database",
  "collectionName": "Users",
  "sqlQuery": "SELECT * FROM x where x.age = {age} and x.gender = {gender}",
  "connection": "COSMOSDB_CONNECTION_STRING"
},
Run Code Online (Sandbox Code Playgroud)

根据此答案,路由约束users/{age=age?}/{gender=gender?}对Web API有效,根据文档, 您可以将任何Web API路径约束与您的参数一起使用.最后,我想向Azure函数发出一个GET请求api/users?age=30&gender=male.那怎么办呢?

Gar*_*son 5

我认为您不能将Cosmos DB绑定到查询参数中定义的值,例如?age=30.至少我还没有在函数文档中看到过这样的例子.

但是你可以将它们绑定到路由参数以实现相同的结果,你已经完成了很多工作.

保持这条路线users/{age}/{gender},你的Cosmos SqlQuery将在调用GET时获取这些路由参数http://yourfunctionhost/yourfunction/users/30/male