问题描述
我无法让 Swashbuckle.AspNetCore (1.0.0) 包生成任何输出.我读了 swagger.json 文件应该写到'~/swagger/docs/v1'.但是,我没有得到任何输出.
我从一个全新的 ASP.NET Core API 项目开始.我应该提到这是 ASP.NET Core 2.API 工作正常,我能够从值控制器中检索值就好了.
我的启动类具有与本文所述完全相同的配置(
同样,这是所有默认模板,但我包含 ValuesController 以供参考...
[Route("api/[controller]")]公共类 ValuesController : 控制器{//获取 api/值[HttpGet]公共 IEnumerable<string>得到(){返回新字符串[] { "value1", "value2" };}//获取 api/values/5[HttpGet("{id}")]公共字符串获取(int id){返回价值";}//POST api/值[HttpPost]公共无效帖子([FromBody]字符串值){}//输入 api/values/5[HttpPut("{id}")]public void Put(int id, [FromBody]string value){}//删除 api/values/5[HttpDelete("{id}")]公共无效删除(int id){}}
我相信您在 Configure 中错过了这两行:
if (env.IsDevelopment()){app.UseDeveloperExceptionPage();//启用中间件以将生成的 Swagger 作为 JSON 端点提供服务.app.UseSwagger();app.UseSwaggerUI(c =>{c.SwaggerEndpoint("v1/swagger.json", "MyAPI V1");});}
要访问 Swagger UI,URL 应该是:
I am having trouble getting the Swashbuckle.AspNetCore (1.0.0) package to generate any output. I read the swagger.json file should be written to '~/swagger/docs/v1'. However, I am not getting any output.
I started with a brand new ASP.NET Core API project. I should mention this is ASP.NET Core 2. The API works, and I am able to retrieve values from the values controller just fine.
My startup class has the configuration exactly as described in this article (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();
}
}
You can see the NuGet references...
Again, this is all the default template, but I include the ValuesController for reference...
[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)
{
}
}
I believe you missed these two lines on your 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");
});
}
To access Swagger UI, the URL should be: http://localhost:XXXX/swagger/
The json can be found at the top of Swagger UI:
这篇关于ASP.NET Core - Swashbuckle 不创建 swagger.json 文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!