码迷,mamicode.com
首页 > Web开发 > 详细

ASP.NET CORE 2.0 Swagger

时间:2018-06-04 18:24:25      阅读:584      评论:0      收藏:0      [点我收藏+]

标签:route   command   develop   ppc   comm   TE   sum   swagger   The   

Package

技术分享图片

添加并配置Swagger中间件

将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; }
    }
}

 

效果

技术分享图片

 

ASP.NET CORE 2.0 Swagger

标签:route   command   develop   ppc   comm   TE   sum   swagger   The   

原文地址:https://www.cnblogs.com/lgxlsm/p/9134715.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!