ASP.NET Core-Swashbuckle不创建swagger.json文件 - c#

我在获取Swashbuckle.AspNetCore(1.0.0)包来生成任何输出时遇到麻烦。我阅读了swagger.json文件,应将其写入“〜/ swagger / docs / v1”。但是,我没有得到任何输出。

我从一个全新的ASP.NET Core API项目开始。我应该提到这是ASP.NET Core2。该API可以正常工作,并且我可以从值控制器中检索值。

我的启动类具有与本文(Swashbuckle.AspNetCore on GitHub)中所述完全相同的配置。

public class Startup
{
    public Startup(IConfiguration configuration)
    {
        Configuration = configuration;
    }

    public IConfiguration Configuration { get; }

    // This method gets called by the runtime. Use this method to add services to the container.
    public void ConfigureServices(IServiceCollection services)
    {
        services.AddMvc();

        services.AddSwaggerGen(c =>
        {
            c.SwaggerDoc("v1", new Info { Title = "My API", Version = "v1" });
        });
    }

    // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
    public void Configure(IApplicationBuilder app, IHostingEnvironment env)
    {
        if (env.IsDevelopment())
        {
            app.UseDeveloperExceptionPage();

            // Enable middleware to serve generated Swagger as a JSON endpoint.
            app.UseSwagger();
            app.UseSwaggerUI(c =>
            {
                c.SwaggerEndpoint("/swagger/v1/swagger.json", "MyAPI V1");
            });
        }
        else
        {
            app.UseExceptionHandler();
        }

        app.UseStatusCodePages();
        app.UseMvc();

        //throw new Exception();
    }
}

您可以看到NuGet参考...

ASP.NET Core-Swashbuckle不创建swagger.json文件 - c#

同样,这是所有默认模板,但我包括ValuesController供参考...

[Route("api/[controller]")]
public class ValuesController : Controller
{
    // GET api/values
    [HttpGet]
    public IEnumerable<string> Get()
    {
        return new string[] { "value1", "value2" };
    }

    // GET api/values/5
    [HttpGet("{id}")]
    public string Get(int id)
    {
        return "value";
    }

    // POST api/values
    [HttpPost]
    public void Post([FromBody]string value)
    {
    }

    // PUT api/values/5
    [HttpPut("{id}")]
    public void Put(int id, [FromBody]string value)
    {
    }

    // DELETE api/values/5
    [HttpDelete("{id}")]
    public void Delete(int id)
    {
    }
}

参考方案

我相信您错过了Configure上的这两行:

if (env.IsDevelopment())
{
    app.UseDeveloperExceptionPage();

    // Enable middleware to serve generated Swagger as a JSON endpoint.
    app.UseSwagger();
    app.UseSwaggerUI(c =>
    {
        c.SwaggerEndpoint("v1/swagger.json", "MyAPI V1");
    });
}

要访问Swagger UI,URL应为:http://localhost:XXXX/swagger/

JSON可以在Swagger UI的顶部找到:

ASP.NET Core-Swashbuckle不创建swagger.json文件 - c#

ASP.NET Core Singleton实例与瞬态实例的性能 - c#

在ASP.NET Core依赖注入中,我只是想知道注册Singleton实例是否会比注册Transient实例更好地提高性能?在我看来,对于Singleton实例,创建新对象和相关对象只需花费一次时间。对于Transient实例,此成本将针对每个服务请求重复。因此Singleton似乎更好。但是,在Singleton上使用Transient时,我们可以获得多…

ASP.NET Core-在Singleton注入上存储库依赖项注入失败 - c#

我正在使用SoapCore为我的ASP.NET Core MVC应用程序创建Web服务。我正在使用Entity Framework Core和简单的存储库模式来获取我的数据库数据。我通过Startup.cs中的.AddSingleton()注入存储库类:services.AddSingleton<IImportRepository, ImportRep…

ASP.NET Core-使用Windows身份验证进行授权 - c#

我已将我的Web API配置为与Windows身份验证一起使用。我的目标实质上是根据用户的Windows帐户来限制控制器中的某些操作。一些将能够执行读取操作,而其他一些将能够执行将写入基础数据库的操作。我找到了大量有关如何设置基于声明的授权的文档,这是我认为我需要走的路。我还没有找到如何使用Windows身份验证进行设置。我想我缺少中间步骤,例如将Windo…

如何在ASP.NET Core Web应用程序中增加JSON反序列化MaxDepth限制 - c#

我们正在将ASP.NET Core 2.1与.NET Framework 4.6.2结合使用。我们有一个客户需要向我们的Web应用程序发送一个很大程度上嵌套的json结构。当他们进行此调用时,我们将输出以下日志并返回错误: 读取器的MaxDepth超过了32。路径“ super.long.path.to property”,第1行,位置42111。”我浏览了…

ASP.NET Core 2.0中的多个身份 - c#

我正在将ASP.NET Core 1.0应用程序迁移到ASP.NET Core 2.0。在我的启动中,我正在配置两个身份:services.AddIdentity<IdentityUser, IdentityRole>(configureIdentity) .AddDefaultTokenProviders() .AddUserStore<…