标签:route command develop ppc comm TE sum swagger The
将Swagger生成器添加到方法中的服务集合中Startup.ConfigureServices
:
public void ConfigureServices(IServiceCollection services) { services.AddMvc(); services.AddSwaggerGen(c => { c.SwaggerDoc("v1", new Info { Title = "WoorldCup Api", Version = "v1" }); // Set the comments path for the Swagger JSON and UI. var xmlFile = $"{Assembly.GetExecutingAssembly().GetName().Name}.xml"; var xmlPath = Path.Combine(AppContext.BaseDirectory, xmlFile); c.IncludeXmlComments(xmlPath); }); }
在该Startup.Configure方法中,启用用于为生成的JSON文档和Swagger UI提供服务的中间件:
public void Configure(IApplicationBuilder app, IHostingEnvironment env) { if (env.IsDevelopment()) { app.UseDeveloperExceptionPage(); } app.UseSwagger(); app.UseSwaggerUI(c => { c.SwaggerEndpoint("/swagger/v1/swagger.json", "WoorldCup Api V1"); }); app.UseMvc(); }
Controller标记
using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.Mvc; using System.ComponentModel; using System.ComponentModel.DataAnnotations; using System.Linq; using WorldCup.Data; namespace WorldCup.Controllers { /// <summary> /// 比赛相关 /// </summary> [Route("api/[controller]")] public class MatchController : Controller { private readonly WorldCupDBContext _worldCupDb; private readonly IHostingEnvironment _hostingEnvironment; public MatchController(WorldCupDBContext worldCupDb) { _worldCupDb = worldCupDb; } /// <summary> /// 获取所有队伍 /// </summary> /// <returns></returns> [HttpGet("GetAllTeam")] public IActionResult GetAllTeam(TodoItem item) { return Ok(new { Result = _worldCupDb.WorldCup_Country.ToList() }); } } public class TodoItem { public long Id { get; set; } [Required] public string Name { get; set; } [DefaultValue(false)] public bool IsComplete { get; set; } } }
效果
标签:route command develop ppc comm TE sum swagger The
原文地址:https://www.cnblogs.com/lgxlsm/p/9134715.html