c# - Azure函数可选 "middle"路由参数

标签 c# .net azure azure-functions

我想在 Azure 函数中将“中间”路由参数设置为可选。例如:

 public static HttpResponseMessage MyFunction([HttpTrigger(AuthorizationLevel.Anonymous, "get", Route = "SomeRoute/{MyOptionalRoute=int?}/AnotherRoute")]HttpRequestMessage req, TraceWriter log,
        int MyOptionalRoute = 0)
    {
       //some magic
    }  

如果我给 MyOptionalValue 一个值,则此方法有效。例如:/SomeRoute/123/AnotherRoute

但如果我不这样做,则返回 404:例如:/SomeRoute/AnotherRoute

有谁知道是否有办法解决这个问题,这样我就不必创建两个单独的函数?我环顾四周,看到的只是人们使用可选的路由参数作为序列中的最后一个参数。也许我只是不知道要谷歌什么关键字或者这是不可能的?

感谢我能得到的所有帮助。

谢谢。

最佳答案

正如您所发现的,Azure 函数尚不支持可选的“中间”路由参数。只有像 SomeRoute/{MyOptionalRoute:int?}/{AnotherRoute:int?} 这样的连续可选参数才有效。

回到正题,用 proxy for function 找到解决方法,看看是否符合您的要求。

proxies.json添加到您的函数项目中,将文件属性复制到输出目录更改为复制如果较新

enter image description here

参见下面的内容,我使用0作为保留数来代替空值。代理将 http://localhost/api/SomeRoute/AnotherRoute 定向到真实的 url http://localhost/api/SomeRoute/0/AnotherRoute,它与以下模式匹配SomeRoute/{MyOptionalRoute:int}/AnotherRoute

{
  "$schema": "http://json.schemastore.org/proxies",
  "proxies": {
    "proxy1": {
      "matchCondition": {
        "methods": [ "GET" ],
        "route": "/api/SomeRoute/AnotherRoute"
      },
      "backendUri": "http://localhost/api/SomeRoute/0/AnotherRoute"
    }
  }
}

关于c# - Azure函数可选 "middle"路由参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51673053/

相关文章:

c# - 参数化查询

.net - 如何使用特定文件启动 Excel 加载项调试器?

azure - 如何使用 Bicep 在现有 vnet 上创建子网阵列?

java - 使用 Java 连接到 Azure 中的 MongoDB

azure - 在 Azure DevOps 中运行 Postman

c# - WPF ListBox 属性绑定(bind)不更新

c# - 使用包装器类模拟 .NET 类

C# 代码长度和性能之间是否存在依赖关系?

c# - 为什么 decimal 不是原始类型?

c# - NetworkStream,是否有类似于 SerialPort 的 DataReceived 的东西? (C#)