Swagger 是一個規範和完整的框架,用於生成、描述、調用和可視化 RESTful 風格的 Web 服務。html
做用:json
1.接口的文檔在線自動生成。api
2.功能測試app
本文轉自:http://www.javashuo.com/article/p-kahbqsxd-u.html框架
今天來嚐嚐鮮。貌似.net core 3.0使用Swagger 4.0.1會報錯,隨手一搜,還沒人寫這個把調試經過的代碼貼一下:dom
依賴包:測試
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 }
控制器:ui
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 }