Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

.Net 6 Ocelot 18.0.0 returns Error 404 when navigating to an URL of the gateway

Tags:

c#

.net

ocelot

I have two API's. One of them is a Gateway (Ocelot + .Net 6) and the other one is a normal .Net 6 API, which I'm going to call Backoffice. On both of them I have a controller with and endpoint 'api/health' that indicates if the API is running and describes the environment. When I call the endpoints of each API, both API's are working.

The endpoints are:

  • http://localhost:5039/api/health
  • http://localhost:5105/api/health

However, when I call the Gateway endpoint that points to the Backoffice API, it returns an 404.

The Gatewayt that redirects to the Backoffice API is:

  • http://localhost:5039/backoffice/api/health

But it returns 404. I can't really understand why. I have the ocelot.json configured and added in the Program.cs file.

And even worse, I can't seem to find any documentation on .Net 6 implementation. .Net 6 now has only a Program.cs and doesn't have a Startup.cs so it confuses a little bit and can't seem to find an example online.

ocelot.Development.json

{
  "Routes": [
    //Backoffice API
    {
      "DownstreamPathTemplate": "/{everything}",
      "DownstreamScheme": "http",
      "DownstreamHostAndPorts": [
        {
          "Host": "localhost",
          "Port": 5105
        }
      ],
      "UpstreamPathTemplate": "/backoffice/{everything}",
      "UpstreamHttpMethod": [ "Get", "Post", "Put", "Delete", "Options" ],
      "Key": "user-transactions"
    }
  ],
  "GlobalConfiguration": {
    "RequestIdKey": "OcRequestId",
    "AdministrationPath": "/administration",
    "BaseUrl": "http://localhost:5039"
  }
}

launchSettings.json

{
  "$schema": "https://json.schemastore.org/launchsettings.json",
  "iisSettings": {
    "windowsAuthentication": false,
    "anonymousAuthentication": true,
    "iisExpress": {
      "applicationUrl": "http://localhost:5039",
      "sslPort": 0
    }
  },
  "profiles": {
    "Dimatur.APIGateway": {
      "commandName": "Project",
      "dotnetRunMessages": true,
      "launchBrowser": true,
      "launchUrl": "swagger",
      "applicationUrl": "http://localhost:5039",
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development"
      }
    },
    "IIS Express": {
      "commandName": "IISExpress",
      "launchBrowser": true,
      "launchUrl": "swagger",
      "applicationUrl": "http://localhost:5039",
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development"
      }
    }
  }
}

Program.cs

using Ocelot.DependencyInjection;
using Ocelot.Middleware;


var builder = WebApplication.CreateBuilder(args);

var env = Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT");
// Add services to the container.

builder.Services.AddControllers();

IConfiguration configuration = builder.Configuration.AddJsonFile($"ocelot.{env}.json", true, true).Build();

builder.Services.AddOcelot(configuration);

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

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

app.UseHttpsRedirection();

app.UseAuthorization();

app.MapControllers();

app.Run();

app.UseOcelot().Wait();

I tried to edit the launchSettings.json on both API's, but never managed to succeed. Also, tried to use version 17.0.0 but it didn't work either (I'm using 18.0.0).

I have added:

IConfiguration configuration = builder.Configuration.AddJsonFile($"ocelot.{env}.json", true, true).Build();

builder.Services.AddOcelot(configuration);

And at the end of the file: app.UseOcelot().Wait();

What I'm expecting is, when I call http://localhost:5039/backoffice/api/health, it should return a JSON. The same one the returns in http://localhost:5105/api/health. But I can't seem to debug what's happening and don't know what to do anymore. When I call the API's directly, everything is working.

like image 930
Joel Vicente Avatar asked Oct 15 '25 21:10

Joel Vicente


1 Answers

put the app.UseOcelot().Wait(); just above the app.UseAuthorization(); and try. enter image description here

like image 55
Adil Saeed Avatar answered Oct 18 '25 10:10

Adil Saeed