【问题标题】:Re-routing Error asp.net Core with Ocelot (7.0.4)使用 Ocelot (7.0.4) 重新路由错误 asp.net Core
【发布时间】:2019-03-20 14:10:33
【问题描述】:
"ReRoutes": [
{
  "DownstreamPathTemplate": "/api/Agent/GetPagedAgents?page={page}",
  "DownstreamScheme": "http",
  "DownstreamHostAndPorts": [
    {
      "Host": "agent.api",
      "Port": 80
    }
  ],
  "UpstreamPathTemplate": "/api/account/user/list/GetPagedAgents?page={page}",
  "UpstreamHttpMethod": []

}]

我正在尝试将我的 UpstreamPathTemplate 从查询字符串重新路由到 DownstreamPathTemplate,

"http://accountmanagement/api/account/user/list/GetPagedAgents?page=1"

这是我发送到我的帐户管理服务的查询字符串,以便使用 ocelot 重新路由到我的代理服务。

这是我在代理服务中用于接收重新路由路径的控制器方法

    [HttpGet]
    [Route("GetPagedAgents")]
    [ProducesResponseType((int)HttpStatusCode.OK)]
    [ProducesResponseType((int)HttpStatusCode.BadRequest)]
    public IActionResult Get(string page, string pageSize, string filter, 
    string sortBy)
    {
        var Result = _agentServices.GetAll(Convert.ToInt32(page), 
Convert.ToInt32(pageSize),filter,sortBy);

          return Ok(Result);
    }

但它不起作用。在我的 OUTPUT 窗口中显示消息:无法找到路径的下游路由:/api/account/user/list/GetPagedAgents,动词:GET

这意味着它把我的 UpstreamPath 作为

 Upstream url path is /api/account/user/list/GetPagedAgents

这里缺少参数。

任何帮助将不胜感激。谢谢

【问题讨论】:

  • 你确定这个网址http://accountmanagement/api/account/user/list/GetPagedAgents?page=1可以达到你的行动吗?

标签: c# asp.net-mvc asp.net-core ocelot


【解决方案1】:

您是否尝试添加[FromQuery] 属性?

public IActionResult Get([FromQuery] string page, [FromQuery] string pageSize, [FromQuery] string filter, [FromQuery]string sortBy)
{
...
}

或者创建一个简单的模型

public class Request
{
   public string Page { get; set; }
   public string PageSize { get; set; }
   public string Filter { get; set; }
   public string SortBy { get; set; }
}

并像使用它

public IActionResult Get([FromQuery] Request request)
{
...
}

【讨论】:

    猜你喜欢
    • 2018-12-19
    • 2020-06-23
    • 2020-04-04
    • 2017-02-03
    • 1970-01-01
    • 1970-01-01
    • 2021-03-28
    • 2021-09-04
    • 2017-11-17
    相关资源
    最近更新 更多