标签:only bho returns star ima generic asp poi png
Swagger 是一个规范和完整的框架,用于生成、描述、调用和可视化 RESTful 风格的 Web 服务。
作用:
1.接口的文档在线自动生成。
2.功能测试
本文转自:https://www.cnblogs.com/datacool/p/11805585.html
今天来尝尝鲜。貌似.net core 3.0使用Swagger 4.0.1会报错,随手一搜,还没人写这个把调试通过的代码贴一下:
依赖包:
1 using Microsoft.AspNetCore.Builder; 2 using Microsoft.AspNetCore.Hosting; 3 using Microsoft.Extensions.Configuration; 4 using Microsoft.Extensions.DependencyInjection; 5 using Microsoft.Extensions.Hosting; 6 using Microsoft.OpenApi.Models; 7 using System; 8 using System.IO; 9 using System.Reflection; 10 11 namespace WebApplication4 12 { 13 public class Startup 14 { 15 public Startup(IConfiguration configuration) 16 { 17 Configuration = configuration; 18 } 19 20 public IConfiguration Configuration { get; } 21 22 // This method gets called by the runtime. Use this method to add services to the container. 23 public void ConfigureServices(IServiceCollection services) 24 { 25 services.AddControllers(); 26 services.AddSwaggerGen(c => 27 { 28 c.SwaggerDoc("v1", new OpenApiInfo { Title = ".Net Core中间件API文档", Version = "v1" }); 29 // 为 Swagger 设置xml文档注释路径 30 var xmlFile = $"{Assembly.GetExecutingAssembly().GetName().Name}.xml"; 31 var xmlPath = Path.Combine(AppContext.BaseDirectory, xmlFile); 32 c.IncludeXmlComments(xmlPath); 33 }); 34 } 35 36 // This method gets called by the runtime. Use this method to configure the HTTP request pipeline. 37 public void Configure(IApplicationBuilder app, IWebHostEnvironment env) 38 { 39 //启用中间件服务生成Swagger 40 app.UseSwagger(); 41 //启用中间件服务生成SwaggerUI,指定Swagger JSON终结点 42 app.UseSwaggerUI(c => 43 { 44 c.SwaggerEndpoint("/swagger/v1/swagger.json", ".Net Core中间件API文档 V1"); 45 c.RoutePrefix = string.Empty;//设置根节点访问 46 }); 47 if (env.IsDevelopment()) 48 { 49 app.UseDeveloperExceptionPage(); 50 } 51 app.UseRouting(); 52 app.UseAuthorization(); 53 app.UseEndpoints(endpoints => 54 { 55 endpoints.MapControllers(); 56 }); 57 } 58 } 59 }
控制器:
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Threading.Tasks; 5 using Microsoft.AspNetCore.Mvc; 6 using Microsoft.Extensions.Logging; 7 8 namespace WebApplication4.Controllers 9 { 10 /// <summary> 11 /// 天气预报API 12 /// </summary> 13 [ApiController] 14 [Route("api/[controller]/[action]")] 15 public class WeatherForecastController : ControllerBase 16 { 17 private static readonly string[] Summaries = new[] 18 { 19 "Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching" 20 }; 21 22 private readonly ILogger<WeatherForecastController> _logger; 23 24 public WeatherForecastController(ILogger<WeatherForecastController> logger) 25 { 26 _logger = logger; 27 } 28 29 /// <summary> 30 /// 获取当前天气 31 /// </summary> 32 /// <param name="size">城市的个数</param> 33 /// <returns></returns> 34 [HttpGet] 35 [HttpPost] 36 public IEnumerable<WeatherForecast> GetWeather(string size = "5") 37 { 38 var rng = new Random(); 39 return Enumerable.Range(1, Convert.ToInt32(size)).Select(index => new WeatherForecast 40 { 41 Date = DateTime.Now.AddDays(index), 42 TemperatureC = rng.Next(-20, 55), 43 Summary = Summaries[rng.Next(Summaries.Length)] 44 }) 45 .ToArray(); 46 } 47 /// <summary> 48 /// 测试方法 49 /// </summary> 50 /// <returns></returns> 51 [HttpGet] 52 public ApiResult Demo() 53 { 54 return new ApiResult 55 { 56 Message = "操作成功!", 57 Success = true, 58 Result = 1 59 }; 60 } 61 } 62 }
netcore3.0 webapi集成Swagger 5.0,Swagger使用
标签:only bho returns star ima generic asp poi png
原文地址:https://www.cnblogs.com/bgh408/p/11849845.html