Ocelot - 下游-上游的占位符不同
Ocelot - Placeholder different in Downstream-Upstream
我有一个 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), from some logics I get the userid that made this call, for example Andrew, and I want to redirect the request to my API http://localhost:44301/api/User/Andrew(我的下游网关)接到电话。
一切都很好,除了我的 API 用户 ID 以 {userid} 的形式出现并且没有用户 ID 值 (Andrew)。
您可以使用 Ocelot 的 Claims Transformation 功能来实现。
例如
"AddQueriesToRequest": {
"LocationId": "Claims[LocationId] > value",
}
我设法用配置中的这段代码做到了:
"DownstreamPathTemplate": "/api/User/{userid}",
"DownstreamScheme": "http",
"ChangeDownstreamPathTemplate": {
"userid": "Claims[userId] > value"
},
"UpstreamPathTemplate": "/api/User/",
我有一个 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), from some logics I get the userid that made this call, for example Andrew, and I want to redirect the request to my API http://localhost:44301/api/User/Andrew(我的下游网关)接到电话。
一切都很好,除了我的 API 用户 ID 以 {userid} 的形式出现并且没有用户 ID 值 (Andrew)。
您可以使用 Ocelot 的 Claims Transformation 功能来实现。
例如
"AddQueriesToRequest": {
"LocationId": "Claims[LocationId] > value",
}
我设法用配置中的这段代码做到了:
"DownstreamPathTemplate": "/api/User/{userid}",
"DownstreamScheme": "http",
"ChangeDownstreamPathTemplate": {
"userid": "Claims[userId] > value"
},
"UpstreamPathTemplate": "/api/User/",