Web Api 2 接口API文檔美化

使用用第三方提供的swgger ui 幫助提升 web api 接口列表的閱讀性,而且能夠在頁面中測試服務接口。css

運行程序以下:html

image

注意:在IE中必須輸入紅色部分。web

而且能夠對方法進行測試。api

image

在開發web api 是能夠寫清楚註釋,而且在文檔中能夠所有的顯示出來。app

在工程中處了安裝Swashbuckle 之外,還會用到Owin,system.web.http.owin庫測試

在WebApi項目工程中安裝:Install-Package Swashbuckle ,安裝成功能後,會在項目中的App_Start文件ui

夾中生成一個文件名爲「SwaggerConfig」的文件。並修改以下:spa

   1:  using System.Web.Http;
   2:  using WebApi;
   3:  using WebActivatorEx;
   4:  using Swashbuckle.Application;
   5:   
   6:  [assembly: PreApplicationStartMethod(typeof(SwaggerConfig), "Register")]
   7:   
   8:  namespace WebApi
   9:  {
  10:      public class SwaggerConfig
  11:      {
  12:          public static void Register()
  13:          {
  14:              Swashbuckle.Bootstrapper.Init(GlobalConfiguration.Configuration);
  15:   
  16:              // NOTE: If you want to customize the generated swagger or UI, use SwaggerSpecConfig and/or SwaggerUiConfig here ...
  17:              SwaggerSpecConfig.Customize(c =>
  18:              {
  19:                  c.IncludeXmlComments(GetXmlCommentsPath());
  20:              });
  21:          }
  22:   
  23:          private static string GetXmlCommentsPath()
  24:          {
  25:              return System.String.Format(@"{0}\bin\WebApi.XML", System.AppDomain.CurrentDomain.BaseDirectory);
  26:          }
  27:      }
  28:  }
 
在工程中添加一個StartUp的文件,代碼以下:
   1:   
   2:  using Microsoft.Owin;
   3:  using Owin;
   4:  using System;
   5:  using System.Collections.Generic;
   6:  using System.Linq;
   7:  using System.Web;
   8:  using System.Web.Http;
   9:   
  10:  [assembly: OwinStartup(typeof(WebApi.Startup))]
  11:  namespace WebApi
  12:  {
  13:      public class Startup
  14:      {
  15:          public void Configuration(IAppBuilder app)
  16:          {
  17:              HttpConfiguration config = new HttpConfiguration();
  18:              WebApiConfig.Register(config);
  19:              Swashbuckle.Bootstrapper.Init(config);
  20:              app.UseWebApi(config);
  21:          }
  22:      }
  23:  }
 
新建一個studentController:
   1:  namespace WebApi.Controllers
   2:  {
   3:      /// <summary>
   4:      /// 用戶接口
   5:      /// </summary>
   6:      public class StudentController : ApiController
   7:      {
   8:          /// <summary>
   9:          /// 獲得全部的學生信息
  10:          /// </summary>
  11:          /// <returns></returns>
  12:          public IEnumerable<StudentModel> Get()
  13:          {
  14:              return new List<StudentModel>();
  15:          }
  16:   
  17:          /// <summary>
  18:          /// 根據學生編號獲得學生信息
  19:          /// </summary>
  20:          /// <param name="Id">學生編號</param>
  21:          /// <returns></returns>
  22:          public StudentModel Get(int Id)
  23:          {
  24:              return new StudentModel { };
  25:          }
  26:   
  27:          /// <summary>
  28:          /// 添加學生
  29:          /// </summary>
  30:          /// <param name="studentModel">學生實體</param>
  31:          /// <remarks>添加一個新的學生</remarks>
  32:          /// <response code="400">Bad request </response>
  33:          /// <response code="500">Internal Server Error</response>
  34:          public void Post(StudentModel studentModel)
  35:          {
  36:          }
  37:   
  38:   
  39:          /// <summary>
  40:          /// 修改學生信息
  41:          /// </summary>
  42:          /// <param name="Id">學生編號</param>
  43:          /// <param name="studentModel">學生實體</param>
  44:         
  45:          [ResponseType(typeof(StudentModel))]
  46:          [ActionName("UpdateStudentById")]
  47:          public void Put(int Id, [Form]string studentModel)
  48:          {
  49:   
  50:          }
  51:   
  52:          /// <summary>
  53:          /// 刪除學生信息
  54:          /// </summary>
  55:          /// <param name="Id">學生編號</param>
  56:          public void Delete(int Id)
  57:          {
  58:          }
  59:   
  60:          /// <summary>
  61:          /// 根據學生姓名獲得學生信息
  62:          /// </summary>
  63:          /// <param name="name">學生姓名</param>
  64:          /// <remarks>dfsafdsa</remarks>
  65:          /// <returns></returns>
  66:          //[Route(Name = "GetStudentByUserName")]
  67:          //[ResponseType(typeof(StudentModel))]
  68:          [HttpGet]
  69:          [ActionName("GetStudentByUserName")]
  70:          public IEnumerable<StudentModel> GetStudentByName(string name)
  71:          {
  72:              return new List<StudentModel>();
  73:          }
  74:      }
  75:  }

   設置工程屬性,在屬性的構建中設置輸出文檔:code

image

這裏的「bin\WebApi.XML」文件名稱和SwaggerConfig文件中的配置保持同樣。orm

相關文章
相關標籤/搜索