在使用 Ocelot 时如何使用 ASP.NET Web API 中的默认控制器来响应请求?

问题描述 投票:0回答:0

我在一个只有 WeatherForecast 控制器的简单 API 项目中将 Ocelot 与 .NET 7 结合使用。我想将所有以“/OtherRequest”开头的请求路由到域“respondingdomain.com”上的外部 API,但在本地使用默认控制器响应“/WeatherForecast”请求。该应用程序在端口 5287

上运行

这是我的 Program.cs 和 Ocelot.json 文件。

using Microsoft.Extensions.Configuration;
using Ocelot.DependencyInjection;
using Ocelot.Middleware;

var builder = WebApplication.CreateBuilder(args);

// Add services to the container.
builder.Configuration.AddJsonFile("ocelot.json", optional: true, reloadOnChange: true).Build();
builder.Services.AddOcelot();
builder.Services.AddControllers();


var app = builder.Build();

// Configure the HTTP request pipeline.

app.UseAuthorization();
app.UseOcelot().Wait();

app.MapControllers();

app.Run();
{
  "Routes": [
    {
      "DownstreamPathTemplate": "/OtherRequest/{everything}",
      "DownstreamScheme": "https",
      "DownstreamHostAndPorts": [
        {
          "Host": "respondingdomain.com",
          "Port": 443
        }
      ],
      "DelegatingHandlers": [
        "OcelotLoggingHandler"
      ],
      "UpstreamPathTemplate": "/OtherRequest/{everything}",
      "UpstreamHttpMethod": [ "GET", "POST", "PUT", "DELETE" ],
      "RouteIsCaseSensitive": false
    },
    {
      "DownstreamPathTemplate": "/weatherforecast",
      "DownstreamScheme": "http",
      "DownstreamHostAndPorts": [
        {
          "Host": "localhost",
          "Port": 5287
        }
      ],
      "UpstreamPathTemplate": "/weatherforecast",
      "UpstreamHttpMethod": [ "GET", "POST", "PUT", "DELETE" ],
      "RouteIsCaseSensitive": false

    }
  ]
  
}

问题是“/WeatherForecast”不工作,并以 503 错误响应。”

我也试过这个 ocelot.json 文件

{
  "Routes": [
     {
      "DownstreamPathTemplate": "/OtherRequest/{everything}",
      "DownstreamScheme": "https",
      "DownstreamHostAndPorts": [
        {
          "Host": "respondingdomain.com",
          "Port": 443
        }
      ],
      "DelegatingHandlers": [
        "OcelotLoggingHandler"
      ],
      "UpstreamPathTemplate": "/OtherRequest/{everything}",
      "UpstreamHttpMethod": [ "GET", "POST", "PUT", "DELETE"],
      "RouteIsCaseSensitive": false
    },
    {
      "DownstreamPathTemplate": "/weatherforecast",
      "DownstreamScheme": "http",
      "DownstreamHostAndPorts": [
        {
          "Host": "localhost",
          "Port": 5287
        }
      ],
      "UpstreamPathTemplate": "/weatherforecast",
      "UpstreamHttpMethod": [ "GET", "POST", "PUT", "DELETE" ],
      "RouteIsCaseSensitive": false

    }

  ],
  "GlobalConfiguration":

  {
    "BaseUrl": "http://localhost:5287"
  }

}

我仍然遇到 503 错误。

请注意,“WeatherForecast”控制器的 GET 方法在没有 Ocelot 的情况下也能正常工作,这是控制器代码:

namespace ApiManager.Ocelot.Controllers
{
    [ApiController]
    [Route("[controller]")]
    public class WeatherForecastController : ControllerBase
    {
        private static readonly string[] Summaries = new[]
        {
        "Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching"
    };

        private readonly ILogger<WeatherForecastController> _logger;

        public WeatherForecastController(ILogger<WeatherForecastController> logger)
        {
            _logger = logger;
        }

        [HttpGet]
        public IEnumerable<WeatherForecast> Get()
        {
            return Enumerable.Range(1, 5).Select(index => new WeatherForecast
            {
                Date = DateOnly.FromDateTime(DateTime.Now.AddDays(index)),
                TemperatureC = Random.Shared.Next(-20, 55),
                Summary = Summaries[Random.Shared.Next(Summaries.Length)]
            })
            .ToArray();
        }
    }
}
asp.net .net-7.0 ocelot
© www.soinside.com 2019 - 2024. All rights reserved.