【问题标题】:Swagger is not generating swagger.jsonSwagger 没有生成 swagger.json
【发布时间】:2019-02-09 06:04:05
【问题描述】:

我在一个解决方案中拥有 asp.net 核心 MVC 项目和单独的 WebApi 项目。我在 github 上的文档之后添加了招摇。这是我的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?}");
        });
    }

这里是 nugets:

WebApi 控制器的属性路由:

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

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

我做错了什么?

提前致谢!

【问题讨论】:

  • 您是否检查了输出文件夹以查看json 文件是否实际存在?
  • 您使用的是 Kestrel 还是其他网络服务器 (iisexpress)。 Configure 中的注释部分不包含 if dev-env 的东西?你有一个发布版本,永远不会像这里一样:*.com/questions/48450262/…
  • @JamieRees,是的,我已经搜索了 json。它不在项目文件夹中。
  • 如果你把.UseMvc() 放在Swagger 之前,它有效吗?我的代码中的顺序是 ) MVC 2) Swagger,但我不知道是否有原因:)
  • @JPHellemons,我尝试过 IIS Express 和 IIS,没有特定于环境的代码

标签: c# asp.net-core swagger asp.net-core-2.1


【解决方案1】:

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

检查下面的代码!!

..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");
            });
        }
    

    也许它可以帮助某人。

    【讨论】:

    • 如何将swagger.json导出到本地物理位置?
    • 只使用端点。例如我的位置是https://localurl/swagger/v1/swagger.json
    【解决方案3】:

    检查您是否正确设置了环境,并且命令 app.UseSwagger 是否不在 if 中以仅在确定的环境(如开发)上执行。

    一个测试解决方案是在任何条件语句之外添加行app.UseSwagger()。

    【讨论】: