【发布时间】:2021-01-14 15:14:52
【问题描述】:
我的 Swagger 文档未正确生成,我有基本信息(头衔、姓名、许可证等),但我的路线上没有文档。
这是 Startup.cs 中的设置:
public void ConfigureServices(IServiceCollection services)
{
services.AddDbContext<APIContext>(options => options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));
services.AddControllersWithViews();
services.AddSwaggerGen(c =>
{
c.SwaggerDoc("v1", new OpenApiInfo
{
Version = "v1",
Title = "OpenWeb Challenge",
Description = "A 'simple' example ASP.NET Core Web API",
TermsOfService = new Uri("https://example.com/terms"),
Contact = new OpenApiContact
{
Name = "Anthony Da Silva Ferreira",
Email = string.Empty,
Url = new Uri("https://twitter.com/spboyer"),
}
});
var xmlFile = $"{Assembly.GetExecutingAssembly().GetName().Name}.xml";
var xmlPath = Path.Combine(AppContext.BaseDirectory, xmlFile);
c.IncludeXmlComments(xmlPath);
});
}
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
app.UseSwagger();
app.UseSwaggerUI(c =>
{
c.SwaggerEndpoint("/swagger/v1/swagger.json", "OpenWebChallenge V1");
});
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseRouting();
app.UseAuthorization();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllerRoute(name: "default",pattern: "{controller=Home}/{action=Index}/{id?}");
});
}
还有招摇的内容:
{
"openapi": "3.0.1",
"info": {
"title": "OpenWeb Challenge",
"description": "A 'simple' example ASP.NET Core Web API",
"termsOfService": "https://example.com/terms",
"contact": {
"name": "Anthony Da Silva Ferreira",
"url": "https://twitter.com/spboyer",
"email": ""
},
"license": {
"name": "Licence",
"url": "https://example.com/license"
},
"version": "v1"
},
"paths": { },
"components": { }
}
控制器示例:
public class ContactsController : Controller
{
private readonly APIContext _context;
public ContactsController(APIContext context)
{
_context = context;
}
// GET: Contacts
public async Task<IActionResult> Index()
{
return View(await _context.Contacts.ToListAsync());
}
// GET: Contacts/Details/5
public async Task<IActionResult> Details(int? id)
{
if (id == null)
{
return NotFound();
}
var contact = await _context.Contacts
.Include(c => c.ContactSkills)
.ThenInclude(cs => cs.Skill)
.AsNoTracking()
.FirstOrDefaultAsync(m => m.Id == id);
if (contact == null)
{
return NotFound();
}
return View(contact);
}
}
我是否缺少任何配置或其他内容?这是我第一次从头开始创建 API。
【问题讨论】:
标签: asp.net-core swagger swashbuckle.aspnetcore