首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >网关ocelot无法运行.NET 6 ASP.NET C#

网关ocelot无法运行.NET 6 ASP.NET C#
EN

Stack Overflow用户
提问于 2022-06-05 23:02:58
回答 1查看 1.5K关注 0票数 2

我正在创建一个带有微服务的项目,现在我希望每次尝试访问https://localhost:4482/gateway/product时都能够用ocelot测试API网关,给我“无法到达这个页面”

ocelot.json:

代码语言:javascript
运行
复制
{
  "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

代码语言:javascript
运行
复制
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();

我错过了一些东西,但不知道它是什么,我检查了下游的路径和上游的路径。如果有任何帮助,我将不胜感激

EN

回答 1

Stack Overflow用户

发布于 2022-06-07 15:28:52

在.net核心6中,从ocelot 17.0.1开始,您必须使用“路由”而不是"ReRoute“

你可以直接把它放在appsettings.json里

我假设在您的解决方案中,您有一个类似于

在这里的样本

appsettings.json

代码语言:javascript
运行
复制
{
  "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

代码语言:javascript
运行
复制
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();

如果有用的话请告诉我。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/72511674

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档