【发布时间】:2018-01-25 19:11:42
【问题描述】:
我无法让 Swashbuckle.AspNetCore (1.0.0) 包生成任何输出。我读了 swagger.json 文件应该写到'~/swagger/docs/v1'。但是,我没有得到任何输出。
我从一个全新的 ASP.NET Core API 项目开始。我应该提到这是 ASP.NET Core 2。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 引用...
同样,这是所有默认模板,但我包含 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)
{
}
}
【问题讨论】:
-
在 Visual Studio 中检查输出
-
它并不总是很明显,但是当您发布时,请确保它在“开发”环境中发布,除非您真的想在发布或生产环境中大摇大摆。
-
谁有AKS的解决方案
标签: c# asp.net-core swagger swashbuckle