【问题标题】:How add Gateway base path on CreatedAtRoute Location Header - URL Rewrite如何在 CreatedAtRoute 位置标头上添加网关基本路径 - URL 重写
【发布时间】:2020-01-31 20:29:03
【问题描述】:

我有一个 Ocelot 网关配置如下:

{
  "ReRoutes": [
    {
      "DownstreamPathTemplate": "/api/{version}/{everything}",
      "DownstreamScheme": "http",
      "DownstreamHostAndPorts": [
        {
          "Host": "localhost",
          "Port": 3010
        }
      ],
      "UpstreamPathTemplate": "/serviceName/api/{version}/{everything}",
      "UpstreamHttpMethod": [ "POST", "PATCH", "PUT", "GET", "DELETE" ]
    }
}

我有以下控制器

[Produces("application/json")]
[Route("api/v1/[controller]")]
[ApiController]
public class NameController : ControllerBase
{
    [HttpPost()]
    public async Task<IActionResult> Create([FromForm]Request request, CancellationToken cancellationToken)
    {
        // create something..

        return CreatedAtRoute("Get", new { id }, string.Empty);
    }

    [HttpGet("{id}", Name = "Get")]
    public async Task<IActionResult> Get([FromRoute] string id, CancellationToken cancellationToken)
    {
        // return something
    }
}

我希望我的 Location 标头为 {domain}/serviceName/api/v1/Name/{id},但它返回的是 {domain}/api/v1/Name/{id}

请问有人知道如何使用 CreatedAtRoute 重写 URL 吗?

【问题讨论】:

    标签: c# .net-core url-rewriting url-routing ocelot


    【解决方案1】:

    我通过阅读 Ocelot 文档here找到了解决方案。

    基本上你需要添加Header转换:

    {
      "ReRoutes": [
        {
          "DownstreamHeaderTransform": {
          "Location": "{DownstreamBaseUrl}, {BaseUrl}/serviceName"
          },
          "HttpHandlerOptions": {
            "AllowAutoRedirect": false
          },
          "DownstreamPathTemplate": "/api/{version}/{everything}",
          "DownstreamScheme": "http",
          "DownstreamHostAndPorts": [
            {
              "Host": "localhost",
              "Port": 3010
            }
          ],
          "UpstreamPathTemplate": "/serviceName/api/{version}/{everything}",
          "UpstreamHttpMethod": [ "POST", "PATCH", "PUT", "GET", "DELETE" ]
        }
      ],
      "GlobalConfiguration": {
        "BaseUrl": "https://api.mybusiness.com"
      } 
    }
    

    更多关于不同环境设置BaseUrl的信息,请查看here

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-09-26
      • 1970-01-01
      • 2012-10-02
      • 1970-01-01
      • 2021-08-03
      • 1970-01-01
      • 2020-06-10
      • 1970-01-01
      相关资源
      最近更新 更多