博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
.net core webapi swagger 分组显示
阅读量:2065 次
发布时间:2019-04-29

本文共 6743 字,大约阅读时间需要 22 分钟。

1.通过nuget安装swagger

2.startup.cs 中的ConfigureServices中添加 c.SwaggerDoc

                c.SwaggerDoc("v1",

                    new OpenApiInfo
                    {
                        Title = "xxx平台",
                        Version = "v1",
                        Contact = new OpenApiContact
                        {
                            Email = "xxx@xxx.com",
                            Name = "xxx",
                            Url = new Uri("http://www.xxx.com")
                        }
                    });

                c.SwaggerDoc("v2",

                   new OpenApiInfo
                   {
                       Title = "xxx平台2",
                       Version = "v2",
                       Contact = new OpenApiContact
                       {
                           Email = "xxx@xxx.com",
                           Name = "xxx",
                           Url = new Uri("http://www.xxx.com")
                       }
                   });

public void ConfigureServices(IServiceCollection services)        {            // 首先取出appsettings中的连接字符串,注意参数名应与appsettings中键一致            var connectionString = AppSettingsHelper.Configuration["DBConnStr:MySQL:ConnectionString"];            // 类型是生成的上下文类名            services.AddDbContext
(options => options.UseMySql(connectionString)); services.AddScoped
(); // services.AddScoped
(); //services.AddControllers().AddNewtonsoftJson(option => // //忽略循环引用 // option.SerializerSettings.ReferenceLoopHandling = ReferenceLoopHandling.Ignore //); services.AddControllers(); services.AddMvc().AddJsonOptions(o => { o.JsonSerializerOptions.ReferenceHandler = ReferenceHandler.Preserve; o.JsonSerializerOptions.MaxDepth = 0; }); services.AddCors(m => m.AddPolicy(Any, a => a.SetIsOriginAllowed(_ => true).AllowAnyMethod().AllowAnyHeader().AllowCredentials())); //注册swagger服务 services.AddSwaggerGen(c => { c.SwaggerDoc("v1", new OpenApiInfo { Title = "xxx平台", Version = "v1", Contact = new OpenApiContact { Email = "xxx@xxx.com", Name = "xxx", Url = new Uri("http://www.xxx.com") } }); c.SwaggerDoc("v2", new OpenApiInfo { Title = "xxx平台2", Version = "v2", Contact = new OpenApiContact { Email = "xxx@xxx.com", Name = "xxx", Url = new Uri("http://www.xxx.com") } }); // 为 Swagger 设置xml文档注释路径 var xmlFile = $"{Assembly.GetExecutingAssembly().GetName().Name}.xml"; var xmlPath = Path.Combine(AppContext.BaseDirectory, xmlFile); c.IncludeXmlComments(xmlPath); c.AddSecurityDefinition("Bearer", new OpenApiSecurityScheme { Description = "请输入OAuth接口返回的Token,前置Bearer。示例:Bearer {Roken}", Name = "Authorization", In = ParameterLocation.Header,//jwt默认存放Authorization信息的位置(请求头中) Type = SecuritySchemeType.ApiKey }); c.AddSecurityRequirement(new OpenApiSecurityRequirement { { new OpenApiSecurityScheme { Reference = new OpenApiReference() { Id = "Bearer", Type = ReferenceType.SecurityScheme } }, Array.Empty
() } }); }); }

3.startup.cs中Configure中添加

             c.SwaggerEndpoint("/swagger/v1/swagger.json", "WLCloudAPI");

                c.SwaggerEndpoint("/swagger/v2/swagger.json", "WLCloudAPI2");

public void Configure(IApplicationBuilder app, IWebHostEnvironment env)        {            if (env.IsDevelopment())            {                app.UseDeveloperExceptionPage();            }            app.UseHttpsRedirection();           // app.UseMvc();            app.UseRouting();            //允许跨域 位置和必须放对,否则加载出错            app.UseCors(Any);            app.UseAuthorization();            app.UseEndpoints(endpoints =>            {                endpoints.MapControllers();            });            //可以将Swagger的UI页面配置在Configure的开发环境之中            app.UseSwagger();            //和Swagger UI            app.UseSwaggerUI(c =>            {                c.SwaggerEndpoint("/swagger/v1/swagger.json", "WLCloudAPI");                c.SwaggerEndpoint("/swagger/v2/swagger.json", "WLCloudAPI2");                c.RoutePrefix = string.Empty;            });        }

4.在controller中增加    [ApiExplorerSettings(GroupName ="v1")]即可分组

[ApiController]    [ApiExplorerSettings(GroupName ="v1")]    [EnableCors("Any")]    [Route("api/[controller]")]    public class testController : Controller    {        private readonly ITestRepository _testRepository;         // 注入ComponanyRepository,就可以直接在控制器中使用其方法了        public testController(ITestRepository testRepository)        {            //var sh = new SHA1Managed();            //byte[] data = sh.ComputeHash(Encoding.Unicode.GetBytes("easyboot"))            //  return Convert.ToBase64String(data, Base64FormattingOptions.None);            _testRepository = testRepository;         }        [HttpGet]        public async Task
> Get1() { return await _testRepository.GetTest(); } } [ApiController] [ApiExplorerSettings(GroupName = "v2")] [EnableCors("Any")] [Route("api/[controller]")] public class RedisController : Controller { // private readonly IRedis _IRedis; // 注入ComponanyRepository,就可以直接在控制器中使用其方法了 //public RedisController(IRedis Redis) //{ // // _IRedis = Redis; //} [HttpGet] public Dictionary
Get(string key) { //var csredis = new CSRedis.CSRedisClient("127.0.0.1:6379,defaultDatabase=0");//,prefix = my_ //string s = csredis.Get(key); //object o = WLCloudAPI.util.RedisHelper.csredis.Get
(key); Dictionary
d= WLCloudAPI.util.RedisHelper.csredis.HGetAll(key); // string s = WLCloudAPI.util.RedisHelper.csredis.Get(key); return d; //csredis.get // return _IRedis.Get1(key); } //[HttpGet] //public async Task
Get2(string key) //{ // var csredis = new CSRedis.CSRedisClient("127.0.0.1:6379,password=123,defaultDatabase=0,prefix=my_"); // return csredis.Get(key); // //csredis.get // // return _IRedis.Get1(key); //} //[HttpPost] //public async Task Get2(string key) //{ // return _IRedis.Get(key); //} }

 

 4 可以看到版本选择

 

转载地址:http://mmumf.baihongyu.com/

你可能感兴趣的文章
中小型园区网络的设计与实现 (一)
查看>>
别人的难题,就是你的价值。
查看>>
中小型园区网络的设计与实现 (二)
查看>>
中小型园区网络的设计与实现 (三)
查看>>
VLAN与子网划分区别
查看>>
Cisco Packet Tracer教程
查看>>
01. 开篇:组建小型局域网
查看>>
02. 交换机的基本配置和管理
查看>>
03. 交换机的Telnet远程登陆配置
查看>>
04. 交换机的端口聚合配置
查看>>
05. 交换机划分Vlan配置
查看>>
06. 三层交换机实现VLAN间路由
查看>>
07. 快速生成树协议
查看>>
08. 路由器的基本配置和Talent配置
查看>>
09. 路由器单臂路由配置
查看>>
10. 路由器静态路由配置
查看>>
路由器动态ip获取不到的处理办法
查看>>
微信小程序-调用-腾讯视频-解决方案
查看>>
giuhub搭建及常用操作
查看>>
phpStudy安装yaf扩展
查看>>