欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

.net core Swagger使用记录

程序员文章站 2022-07-02 14:50:25
...

1. NuGet下载:Swashbuckle.AspNetCore

.net core Swagger使用记录

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