3

I'm creating a project with microservices and now I want to be able to test the API gateway with ocelot API every time I try to access https://localhost:4482/gateway/product give me "can't reach this page"

ocelot.json:

{
  "ReRoutes": [
    {
      "DownstreamPathTemplate": "/api/product",
      "DownstreamScheme": "https",
      "DownstreamHostAndPorts": [
        {
          "Host": "localhost",
          "Port": 44
        }
      ],
      "UpstreamPathTemplate": "/gateway/product",
      "UpstreamHttpMethod": [ "GET"]
    },
    {
      "DownstreamPathTemplate": "/api/Order",
      "DownstreamScheme": "https",
      "DownstreamHostAndPorts": [
        {
          "Host": "localhost",
          "Port": 44
        }
      ],
      "UpstreamPathTemplate": "/gateway/Order",
      "UpstreamHttpMethod": [ "GET", "PUT", "POST" ]
    }
  ]
}

Gateway.Program.cs

using Ocelot.DependencyInjection;
using Ocelot.Middleware;



var builder = WebApplication.CreateBuilder(args);

builder.Configuration.AddJsonFile("ocelot.json");
// Add services to the container.


builder.Services.AddControllers();

// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();
builder.Services.AddOcelot();
var app = builder.Build();

// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
    app.UseSwagger();
    app.UseSwaggerUI();
}

app.UseHttpsRedirection();
app.UseOcelot().Wait();

app.UseAuthorization();

app.MapControllers();

app.Run();

I'm missing something but can't figure out what it is, I have checked the downstream path and upstream path. I would be grateful for any help

5
  • Your configuration is set to port 44, try doing localhost:44 Commented Jun 6, 2022 at 0:11
  • @hallie it should be the "sslPort" => "4482" in the launchsettings.json of the Gateway service to get the results directly from the microservice (which is located at localhost:44) Commented Jun 6, 2022 at 0:28
  • Are you able to make a GET request to the downstream service at https://localhost:44/api/product and receive product using postman or a browser? If you can then probably there may be an issue with your ocelot config in the gateway. Commented Jun 6, 2022 at 3:25
  • Not familiar with Ocelot but wondering do you also need to specify the downstream HttpMethod like you have the UpstreamHttpMethod. Something like "DownstreamHttpMethod": ["GET"] Commented Jun 6, 2022 at 3:34
  • 1
    Rename ReRoutes to Routes Commented Jun 6, 2022 at 7:35

1 Answer 1

4

In .net core 6, from ocelot 17.0.1 you have to use "Routes" instead "ReRoute"

and you can put it directly in the appsettings.json

I assume that under your solution you have a project similar to this

enter image description here

here the samples

appsettings.json

{
  "Logging": {
    "LogLevel": {
      "Default": "Information",
      "Microsoft.AspNetCore": "Warning"
    }
  },
  "AllowedHosts": "*",
  "Routes": [
    {
      "DownstreamPathTemplate": "/api/product",
      "DownstreamScheme": "https",
      "DownstreamHostAndPorts": [
        {
          "Host": "localhost",
          "Port": 44
        }
      ],
      "UpstreamPathTemplate": "/gateway/product",
      "UpstreamHttpMethod": [ "GET" ]
    },
    {
      "DownstreamPathTemplate": "/api/Order",
      "DownstreamScheme": "https",
      "DownstreamHostAndPorts": [
        {
          "Host": "localhost",
          "Port": 44
        }
      ],
      "UpstreamPathTemplate": "/gateway/Order",
      "UpstreamHttpMethod": [ "GET", "PUT", "POST" ]
    }
  ]
}

program.cs

using Ocelot.DependencyInjection;
using Ocelot.Middleware;

var builder = WebApplication.CreateBuilder(args);

// Add services to the container.

builder.Services.AddControllers();
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();
builder.Services.AddOcelot();

var app = builder.Build();

// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
    app.UseSwagger();
    app.UseSwaggerUI();
}

app.UseOcelot();

app.UseHttpsRedirection();

app.UseAuthorization();

app.MapControllers();

app.Run();

let me know if it works.

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.