本文包括两个部分:

  1. webapi中使用swagger
  2. 修改webapi的路由和默认参数
    • webapi中使用swagger
    • 项目打开之后,选择 引用,右键,管理nuget程序包

    • 浏览,搜索swagger,选择第一个swashbuckle,安装

    • 安装好之后,右键项目,选择属性,生成,在下面的输出那里勾选:xml文档文件,如果没有自动填充好路径,需要自己填写一下,文件名可以自己

    • 打开app_start文件夹下的swaggerconfig.cs文件,新增一个如下方法:

    • private static string getxmlcommentspath()
      {
      return system.string.format(@”{0}\bin\coldairarrow.webapi.xml”, system.appdomain.currentdomain.basedirectory);
      }

    • 搜索getxmlcommentspath,下面能搜到已经注释了,自己把注释放开,要是没搜到,就自己手动写一下c.includexmlcomments(getxmlcommentspath());注意要写在register方法里面

    • 运行项目,在根路径后面直接加swagger,就会自动跳转到文档,如:http://localhost:13520/swagger/ui/index,能看到我们写的一些注释