Swagger 未生成 swagger.json

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

我在一个解决方案中拥有 asp.net core MVC 项目和单独的 WebApi 项目。我按照 github 上的文档添加了 swagger。这是我的mvc项目的Startup.cs:

public void ConfigureServices(IServiceCollection services)
    {
        //...
        // Adding controllers from WebApi:
        var controllerAssembly = Assembly.Load(new AssemblyName("WebApi"));
        services.AddMvc(o =>
            {
                o.Filters.Add<GlobalExceptionFilter>();
                o.Filters.Add<GlobalLoggingFilter>();
            })
            .AddApplicationPart(controllerAssembly)
            .AddJsonOptions(options =>
            {
                options.SerializerSettings.ReferenceLoopHandling = ReferenceLoopHandling.Ignore;
            });

        services.AddSwaggerGen(c =>
        {
            //The generated Swagger JSON file will have these properties.
            c.SwaggerDoc("v1", new Info
            {
                Title = "Swagger XML Api Demo",
                Version = "v1",
            });
        });

        //...
    }

    public void Configure(IApplicationBuilder app, IHostingEnvironment env)
    {
        //...
        app.UseSwagger();
        app.UseSwaggerUI(c =>
        {
            c.SwaggerEndpoint("/swagger/v1/swagger.json", "Swagger XML Api Demo v1");
        });

        //...

        app.UseMvc(routes =>
        {
            // ...
            routes.MapRoute(
                name: "default",
                template: "{controller=Home}/{action=Index}/{id?}");
        });
    }

以下是要点:

WebApi 控制器属性路由:

[Route("api/[controller]")]
public class CategoriesController : Controller
{
    // ...
    [HttpGet]
    public async Task<IActionResult> Get()
    {
        return Ok(await _repo.GetCategoriesEagerAsync());
    }
    // ...
}

当我尝试访问 /swagger 时,它找不到 /swagger/v1/swagger.json:

我做错了什么?

提前致谢!

c# asp.net-core swagger asp.net-core-2.1
5个回答
3
投票

我在这个问题上被困了几个小时......我找到了原因......

检查下面的代码!!

..Startup.cs..

public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
    if (env.IsDevelopment())
    {
        app.UseDeveloperExceptionPage();
    }
    else
    {
        app.UseHsts();
    }

    app.UseHttpsRedirection();
    app.UseMvc();
    app.UseSwagger(); // if I remove this line, do not work !
    app.UseSwaggerUi3();
}

2
投票

我也想在这里添加我的经验。 我已将

configureServices
中的版本指定为
V1
(注意大写的
V
)和

public void ConfigureServices(IServiceCollection services)
    {
        services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
        services.AddSwaggerGen(c =>
        {
            c.SwaggerDoc("v1", //this v was in caps earlier
            new Swashbuckle.AspNetCore.Swagger.Info
            {
                Version = "v1",//this v was in caps earlier
                Title = "Tiny Blog API",
                Description = "A simple and easy blog which anyone love to blog."
            });
        });
        //Other statements
    }

然后在

configure
方法中它是在小情况下

public void Configure(IApplicationBuilder app, IHostingEnvironment env)
    {
        if (env.IsDevelopment())
        {
            app.UseDeveloperExceptionPage();
        }

        app.UseMvc();
        app.UseSwagger();
        app.UseSwaggerUI(c =>
        {
            c.SwaggerEndpoint("/swagger/v1/swagger.json", "Tiny Blog V1");
        });
    }

也许它可以帮助某人。


0
投票

检查环境设置是否正确,命令

app.UseSwagger
是否不在 if 内,仅在开发等确定的环境中执行。

测试解决方案是在任何条件语句之外添加行

app.UseSwagger(
)。


0
投票

这是一些使用 .net core 6 的示例代码:

using Microsoft.OpenApi.Models;

var builder = WebApplication.CreateBuilder(args);
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen(s =>
{
    s.SwaggerDoc("v1", new OpenApiInfo { Title = "Service Manager API", Description = "API Docs for the Service Manager Layer.", Version = "v1"});
});

var app = builder.Build();

app.MapGet("/", () => "Hello World!");
app.UseSwagger();
app.UseSwaggerUI(c =>
{
    c.SwaggerEndpoint("/swagger/v1/swagger.json", "Service Manager API V1");
});

app.Run();

我只是因为没有在路径中添加“/”而遇到了问题(/swagger/v1...而不是swagger/v1/...


0
投票

如果您缺少在操作方法之上添加 [HttpGet(Name = "")] ,那么这还有一个原因。

如果这对任何人有帮助。

© www.soinside.com 2019 - 2024. All rights reserved.