Swagger没有生成swagger.json [英] Swagger is not generating swagger.json

查看:2629
本文介绍了Swagger没有生成swagger.json的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在一个解决方案中,我拥有asp.net核心MVC项目和单独的WebApi项目.我在github上的文档后添加了招摇.这是我的mvc项目的Startup.cs:

I have the asp.net core MVC project and separate WebApi project in one solution. I'm adding the swagger following the documentation on github. Here is my Startup.cs of mvc project:

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控制器的属性路由:

The WebApi controllers Attribute routing:

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

当我尝试转到/swagger时,找不到/swagger/v1/swagger.json:

When I'm trying to go to /swagger it doesn't find the /swagger/v1/swagger.json:

我做错了什么?

提前谢谢!

推荐答案

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

I was stuck on this problem for hours... and I found the reason...

检查下面的代码!

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

这篇关于Swagger没有生成swagger.json的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆