簡體   English   中英

網關 ocelot API 不起作用 .NET 6 ASP.NET C#

[英]gateway ocelot API doesn't work .NET 6 ASP.NET C#

我正在使用微服務創建一個項目,現在我希望每次嘗試訪問https://localhost:4482/gateway/product時都能使用 ocelot API 測試 API 網關給我“無法訪問此頁面”

豹貓.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" ]
    }
  ]
}

網關程序.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();

我遺漏了一些東西,但無法弄清楚它是什么,我檢查了下游路徑和上游路徑。 我將不勝感激

在 .net core 6 中,從 ocelot 17.0.1 開始,您必須使用“Routes”而不是“ReRoute”

並且可以直接放在appsettings.json中

我假設在您的解決方案下,您有一個類似於此的項目

在此處輸入圖像描述

這里的樣本

應用設置.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" ] } ] }

程序.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();

讓我知道它是否有效。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM