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?

0

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.