简体   繁体   English

使用 ASP.NET Core 6 Swagger UI 时出错 Web API 自定义路由

[英]Getting error for Swagger UI with an ASP.NET Core 6 Web API with custom routing

I have custom routing for my ASP.NET Core 6 Web API:我为我的 ASP.NET Core 6 Web API 定制了路由:

[ApiController]
[Route("ForecastService")]
public class WeatherForecastController : ControllerBase
{
    [HttpGet("forecast")]
    public IEnumerable<string> Get1()
    {
        return new[] { "forecast" };
    }
}

I have made this change in the Swagger configuration:我在 Swagger 配置中进行了此更改:

 builder.Services.AddControllers();
        // 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.UseSwagger(c => c.RouteTemplate = "ForecastService/{documentName}/swagger.json");
            app.UseSwaggerUI(c =>
            {
                c.SwaggerEndpoint("ForecastService/v1/swagger.json", "Forcast API");
                c.RoutePrefix = "ForecastService/swagger";
            });
        }

Now while trying to run browse to http://localhost:5113/ForecastService/swagger/index.html , I am getting an error:现在,在尝试运行浏览至http://localhost:5113/ForecastService/swagger/index.html时,出现错误:

Not found http://localhost:5113/ForecastService/swagger/ForecastService/v1/swagger.json找不到 http://localhost:5113/ForecastService/swagger/ForecastService/v1/swagger.json

What am I missing here?我在这里错过了什么?

在此处输入图像描述

Change your configuration for swagger like below:更改 swagger 的配置,如下所示:

//miss the swagger after ForecastService.....
app.UseSwagger(c => c.RouteTemplate = "ForecastService/swagger/{documentName}/swagger.json");
app.UseSwaggerUI(c =>
{
    //miss the `/` in the beginning and also miss the swagger after ForecastService...
    c.SwaggerEndpoint("/ForecastService/swagger/v1/swagger.json", "Forcast API");
    c.RoutePrefix = "ForecastService/swagger";
});

modify below lines as将以下行修改为

    app.UseSwagger();
    app.UseSwaggerUI();

Use URL as使用 URL 作为

http://localhost:5113/swagger

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM