本文介绍了404尝试将上游路径路由到Ocelot中的下游路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在将传入的HTTP请求转发到下游路径时,我遇到此警告/错误.

I am facing this warning/error while forwarding the incoming http request to the downstream path.

Program.cs

Program.cs

public class Program
{
    public static void Main(string[] args)
    {
        CreateWebHostBuilder(args).Build().Run();
    }

    public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
       WebHost.CreateDefaultBuilder(args)
       .ConfigureAppConfiguration((host, config) =>
       {
           config.AddJsonFile("ocelot.json");
       })
    .UseStartup<Startup>();
}

Startup.cs

Startup.cs

public IConfiguration Configuration { get; }

public Startup(IConfiguration configuration)
{
    Configuration = configuration;
}

// This method gets called by the runtime. Use this method to add services to the container.
// For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?LinkID=398940
public void ConfigureServices(IServiceCollection services)
{
    services.AddOcelot(Configuration);
}

// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public async void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
    if (env.IsDevelopment())
    {
        app.UseDeveloperExceptionPage();
    }

    await app.UseOcelot();

    app.Run(async (context) =>
    {
        await context.Response.WriteAsync("Hello World!");
    });
}

ocelot.json

ocelot.json

{
  "ReRoutes": [
    {
      "DownstreamPathTemplate": "api/department",
      "DownstreamScheme": "http",
      "DownstreamHostAndPorts": [
        {
          "Host": "localhost",
          "Port": 44388
        }
      ],
      "UpstreamPathTemplate": "/getDepartment",
      "UpstreamHttpMethod": [
        "Get"
      ]
    }
  ],
  "GlobalConfiguration": {
    "BaseUrl": "http://localhost:5000"
  }
}

我在此出现错误404.

I am getting Error 404 in this.

推荐答案

如果使用的是最新版本(16.0.0),请在ocelot.json中将"ReRoutes"更改为"Routes".

If you are using the latest version (16.0.0), change "ReRoutes" to "Routes" in your ocelot.json.

我遇到了同样的问题,然后遇到了这个拉取请求,解释说它已更改为与新的Microsoft反向代理项目(YARP)相匹配.他们的文档需要更新. https://github.com/ThreeMammals/Ocelot/pull/1239

I was having the same problem and then came across this pull request explaining it had been changed to match up with a new Microsoft reverse proxy project (YARP). Their documentation needs updating.https://github.com/ThreeMammals/Ocelot/pull/1239

这篇关于404尝试将上游路径路由到Ocelot中的下游路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-18 15:51