.Net Core WebApi 添加Swagger和Redis缓存
一、添加Swagger
步骤一:Nuget添加引用:
1、Swashbuckle.AspNetCore 【Swagger包】
2、Microsoft.Extensions.Caching.Redis 【Redis缓存包】
步骤二:修改starup配置文件
1 //开启缓存 2 services.AddDistributedMemoryCache(); 3 4 //添加Redis缓存配置 5 services.AddStackExchangeRedisCache(options => 6 { 7 options.Configuration = "127.0.0.1:6379"; 8 options.InstanceName = "mvc"; 9 }); 10 11 //配置Swagger 12 //注册Swagger生成器,定义一个Swagger 文档 13 services.AddSwaggerGen(c => 14 { 15 c.SwaggerDoc("v1", new Microsoft.OpenApi.Models.OpenApiInfo 16 { 17 Version = "v1", 18 Title = "接口文档", 19 Description = "RESTful API" 20 }); 21 // 为 Swagger 设置xml文档注释路径 22 var xmlFile = $"{Assembly.GetExecutingAssembly().GetName().Name}.xml"; 23 var xmlPath = Path.Combine(AppContext.BaseDirectory, xmlFile); 24 c.IncludeXmlComments(xmlPath); 25 });
1 //启用中间件服务生成Swagger 2 app.UseSwagger(); 3 //启用中间件服务生成Swagger,指定Swagger JSON终结点 4 app.UseSwaggerUI(c => 5 { 6 c.SwaggerEndpoint("/swagger/v1/swagger.json", "Web App V1"); 7 c.RoutePrefix = string.Empty;//设置根节点访问 8 });
步骤三:控制器方法
步骤四:属性配置
步骤五:效果
Redis效果: