Search code examples
asp.net-web-apiswaggerapi-gatewayocelot

Mapping API with some Microservices with Ocelot duplicates controller name


i'm asking for help here because i'm trying to make an API Gateway with Ocelot and when I map the services with this

{
      "UpstreamPathTemplate": "/api/Product/{everything}",
      "UpstreamHttpMethod": [ "Post", "Get" ],

      "DownstreamScheme": "http",
      "DownstreamHostAndPorts": [
        {
          "Host": "localhost",
          "Port": 6666
        }
      ],
      "DownstreamPathTemplate": "/api/{everything}",
      "SwaggerKey": "product_api"
    }

And the controller starts with this

[Route("api/[controller]")]
    [ApiController]
    public class ProductController : Controller
    {
        IConfiguration _config;

        [NonAction]
        public IActionResult Index()
        {
            return View();
        }

        [HttpGet]
        public ProductResponse GetProduct([FromQuery] int item_id, [FromHeader] string access_token)

Ocelot Swagger shows it like this /api/Product/Product/StoreStock

With the "Product" duplicated, I guess it is because I map api/Product/{everything} and picks the Product from there and the Product from the controller name But I can't use api/{everything} for every UpstreamPathTemplate because Ocelot doesn't let me do it.

Is there any way to fix the route or to map it to the api/{controllername} or something?

I tried mapping all to api/{everything} but Ocelot throw exception with that


Solution

  • It is solved. I fixed it changing "DownstreamPathTemplate": "/api/{everything}" to "DownstreamPathTemplate": "/api/Product/{everything}" and routes now are correct