1

我有一个 API 网关(C#,net.Core 3.1,Ocelot),一切正常,但现在我正在尝试为上游和下游配置不同的路由,因为我的网关在此过程中检索信息并将此信息发送到最终API。

在上游我没有占位符 {userid},但我想在下游有它。

这是我的 ocelot.json:

  "DownstreamPathTemplate": "/api/User/{userid}",
  "DownstreamScheme": "http",
  "DownstreamHostAndPorts": [
    {
      "Host": "localhost",
      "Port": 44301
    }
  ],
  "UpstreamPathTemplate": "/api/User/",
  "UpstreamHttpMethod": [ "Get" ],

还有我如何在中间件中添加占位符值:

   if (context.DownstreamRequest.OriginalString.Contains("User"))
   {
       context.DownstreamRequest.AbsolutePath = 
       context.DownstreamRequest.AbsolutePath + userid; //this variable is valued before
   }

所以,为了更清楚,这里有一个例子:

我在http://localhost:44358/api/User/ (mygateway Upstream) 被调用,从某些逻辑中我得到了进行此调用的用户 ID,例如 Andrew,我想将请求重定向到我的 API http:/ /localhost:44301/api/User/Andrew(mygateway 下游)。

一切都很好,除了在我的 API 中,用户 ID 以 {userid} 的形式出现并且没有用户 ID 值(安德鲁)。

4

2 回答 2

2

您可以使用Ocelot 的声明转换功能来实现。

例如

"AddQueriesToRequest": {
"LocationId": "Claims[LocationId] > value",
}
于 2020-03-26T16:18:13.060 回答
1

我设法在配置中使用以下代码来做到这一点:

"DownstreamPathTemplate": "/api/User/{userid}",
"DownstreamScheme": "http",
"ChangeDownstreamPathTemplate": {
    "userid": "Claims[userId] > value"
  },
"UpstreamPathTemplate": "/api/User/",
于 2020-03-27T16:01:13.297 回答