0

I have an Ocelot Gateway configuration as follow:

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

and I have the following controller

[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
    }
}

I want my Location header to be {domain}/serviceName/api/v1/Name/{id}, however it is returning {domain}/api/v1/Name/{id}.

Anyone knows how can I do a URL rewrite with CreatedAtRoute, please?

Renato Pereira
  • 834
  • 2
  • 9
  • 22

1 Answers1

1

I found the solution by reading the Ocelot documentation here.

Basically you need to add Header transformation:

{
  "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"
  } 
}

for more information about setting BaseUrl for different environments, please see here.

Renato Pereira
  • 834
  • 2
  • 9
  • 22