.net core Swagger使用记录

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

 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值