• WebApi集成Swagger


    1.新建一个WebApi空项目

    2.新建一个Person实体类:

    public class Person
        {
            public int ID { get; set; }
            public string UserName { get; set; }
            public string Password { get; set; }
        }
    View Code

    3.新建一个PersonAPI控制器(PersonController)

    public class PersonController : ApiController
        {
            /// <summary>
            /// 根据ID获取单个人的详细信息
            /// </summary>
            /// <param name="id">人员ID</param>
            /// <returns></returns>
            [HttpGet, Route("person/GetPerson")]
            public Person GetPerson(int id)
            {
                return new Person { ID = id, UserName = "张三", Password = "admin888" };
            }
            /// <summary>
            /// 获取人员列表信息
            /// </summary>
            /// <returns></returns>
            [HttpGet, Route("person/GetPersons")]
            public List<Person> GetPersons()
            {
                List<Person> list = new List<Person> { 
                    new Person{ ID = 3, UserName = "张三", Password = "admin888"},
                    new Person{ ID = 4, UserName = "李四", Password = "admin888"},
                    new Person{ ID = 5, UserName = "王五", Password = "admin888"},
                    new Person{ ID = 6, UserName = "小七", Password = "admin888"}
                };
                return list;
            }
    
            /// <summary>
            /// 添加人员
            /// </summary>
            /// <param name="product"></param>
            /// <returns></returns>
            [HttpPost, Route("person/add")]
            public int AddProduct(Person person)
            {
                throw new NotImplementedException();
            }
    
            /// <summary>
            /// 更新删除人员
            /// </summary>
            /// <param name="personId"></param>
            /// <param name="person"></param>
            [HttpPost, Route("person/update")]
            public void UpdatePerson(int personId, Person person)
            {
                throw new NotImplementedException();
            }
    
            /// <summary>
            /// 删除人员
            /// </summary>
            /// <param name="personId"></param>
            [HttpDelete, Route("product/delete")]
            public void DeletePerson(int personId)
            {
                throw new NotImplementedException();
            }
        }
    View Code

    4.通过NuGet程序包安装swashbuckle程序包

    5.安装后App_Start目录下会生成一个SwaggerConfig.cs文件,修改此文件:

    取消c.IncludeXmlComments(GetXmlCommentsPath())的注释,同时添加GetXmlCommentsPath函数

    private static string GetXmlCommentsPath()
            {
                return string.Format(@"{0}inXXXX.XML", AppDomain.CurrentDomain.BaseDirectory);
            }
    View Code

    其中"XXX.xml"为"你的项目名.xml"
    6.右键项目-属性-生成,勾选"xml文档文件"

    7.好了,生成好之后通过http://主机/swagger/ui/index#/ 地址就可以访问了

  • 相关阅读:
    [精华] Linux内存:内存管理的实质
    阿里巴巴笔试题
    LINUX内核经典面试题(转)
    常见网络编程面试题答案征集与面试题(收集)
    转使用jQuery Ajax的内存回收
    模式识别与机器学习
    vc2010 学习笔记2
    vc2010 学习笔记3
    Android高级编程(笔记)第6章 数据存储、检索和共享—3SQLite数据库
    Android高级编程(笔记)第6章 数据存储、检索和共享1共享Preference
  • 原文地址:https://www.cnblogs.com/myindex/p/5464909.html
Copyright © 2020-2023  润新知