1. NuGet下载:Swashbuckle.AspNetCore
2. 修改Startup.cs文件
services.AddSwaggerGen(options =>
{
options.SwaggerDoc("OneselfAPI", new OpenApiInfo { Title = "私有API帮助文档", Version = "v1" });
options.SwaggerDoc("OpenAPI", new OpenApiInfo { Title = "开放API帮助文档", Version = "v1" });
options.EnableAnnotations();
});
services.AddSwaggerGenNewtonsoftSupport();
//Swagger
app.UseSwagger(c =>
{
c.RouteTemplate = "/api-docs/{documentName}.json";
});
app.UseSwaggerUI(c =>
{
c.DocumentTitle = "文档";
c.RoutePrefix = "ApiHelp";//修改地址 默认 /swagger
c.SwaggerEndpoint("/api-docs/OneselfAPI.json", "OneselfAPI");
c.SwaggerEndpoint("/api-docs/OpenAPI.json", "OpenAPI");
c.InjectStylesheet("/swagger/index.css");//样式
c.InjectJavascript("/swagger/index.js");//js
//需要将文件添加为嵌入式资源,文件的属性-生成操作-嵌入的资源
c.IndexStream = () => GetType().Assembly.GetManifestResourceStream("netcore测试.wwwroot.swagger.api-docs.html");
//下面这部分未使用
c.OAuthClientId("test-id");
c.OAuthClientSecret("test-secret");
c.OAuthRealm("test-realm");
c.OAuthAppName("test-app");
c.OAuthScopeSeparator(" ");
c.OAuthAdditionalQueryStringParams(new Dictionary<string, string> { { "foo", "bar" } });
c.OAuthUseBasicAuthenticationWithAccessCodeGrant();
});
3. 使用
[HttpPost, Route("[controller]/[action]")]
[ApiExplorerSettings(GroupName = "OneselfAPI")]
[SwaggerOperation(Summary = "测试返回ok", Description = "此处说明", OperationId = "CreateProduct",Tags = new[] { "v1" })]
//获取二维码
public async Task<JsonResult> test(string id)
{
return Json(id);
}
参考资料: https://github.com/domaindrivendev/Swashbuckle.AspNetCore