使用用第三方提供的swgger ui 帮助提高 web api 接口列表的阅读性,并且可以在页面中测试服务接口。

运行程序如下:

注意:在IE中必须输入红色部分。

并且可以对方法进行测试。

在开发web api 是可以写清楚注释,并且在文档中可以全部的显示出来。

在工程中处了安装Swashbuckle 以外,还会用到Owin,system.web.http.owin库

在WebApi项目工程中安装:Install-Package Swashbuckle ,安装成功能后,会在项目中的App_Start文件

夹中生成一个文件名为“SwaggerConfig”的文件。并修改如下:

   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:  }

.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }

.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }

.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }

   设置工程属性,在属性的构建中设置输出文档:

这里的“bin\WebApi.XML”文件名称和SwaggerConfig文件中的配置保持一样。

Web Api 2 接口API文档美化的更多相关文章

  1. Web Api 自动生成帮助文档

    Web Api 自动生成帮助文档   新建Web Api项目之后,会在首页有API的导航菜单,点击即可看到API帮助文档,不过很遗憾,Description 是没有内容的. 怎么办呢? 第一步: 如果 ...

  2. Spring 5 中函数式web开发中的swagger文档

    Spring 5 中一个非常重要的更新就是增加了响应式web开发WebFlux,并且推荐使用函数式风格(RouterFunction和 HandlerFunction)来开发WebFlux.对于之前主 ...

  3. 后端编写Swagger接口管理文档

    Swagger接口管理文档 访问接口文档的网页:http://localhost:8080/swagger-ui/index.html 导入依赖 <dependency> <grou ...

  4. 支付宝接口使用文档说明 支付宝异步通知(notify_url)与return_url.

    支付宝接口使用文档说明 支付宝异步通知(notify_url)与return_url. 现支付宝的通知有两类. A服务器通知,对应的参数为notify_url,支付宝通知使用POST方式 B页面跳转通 ...

  5. Java实现web在线预览office文档与pdf文档实例

    https://yq.aliyun.com/ziliao/1768?spm=5176.8246799.blogcont.24.1PxYoX 摘要: 本文讲的是Java实现web在线预览office文档 ...

  6. (转)WEB页面导出为Word文档后分页&横向打印的方法

    <html>    <HEAD>        <title>WEB页面导出为Word文档后分页&横向打印的方法 </title>    < ...

  7. Web 前端 UI 组件库文档自动化方案 All In One

    Web 前端 UI 组件库文档自动化方案 All In One 需求 自动化 动态 好用 markdown element-ui 中示例和说明按照一定规则写在md文件中,调用md-loader将md文 ...

  8. ASP.NET Web Api 2 接口API文档美化之Swagger

    使用第三方提供的swgger ui 可有效提高 web api 接口列表的阅读性,并且可以在页面中测试服务接口. 但本人在查阅大量资料并进行编码测试后,发现大部分的swagger实例并不能有效运行.例 ...

  9. Web API 自动生成帮助文档并使用Web API Test Client 测试

    之前在项目中有用到webapi对外提供接口,发现在项目中有根据webapi的方法和注释自动生成帮助文档,还可以测试webapi方法,功能很是强大,现拿出来与大家分享一下. 先看一下生成的webapi文 ...

随机推荐

  1. Oracle not in子连接查询不到值的问题(not in 不能查询null数据)

    前几天在项目中,做数据导入时,发现not in和in 查出来的条数不互补.ATABLE2明明中有些记录在ATABLE3中不存在,但是not in时查不出记录. CREATE TABLE ATABLE2 ...

  2. 使用Windows Azure的VM安装和配置CDH搭建Hadoop集群

    本文主要内容是使用Windows Azure的VIRTUAL MACHINES和NETWORKS服务安装CDH (Cloudera Distribution Including Apache Hado ...

  3. NOIP2000单词接龙[DFS]

    题目描述 单词接龙是一个与我们经常玩的成语接龙相类似的游戏,现在我们已知一组单词,且给定一个开头的字母,要求出以这个字母开头的最长的“龙”(每个单词都最多在“龙”中出现两次),在两个单词相连时,其重合 ...

  4. Ac日记——大整数减法 openjudge 1.6 11

    11:大整数减法 总时间限制:  1000ms 内存限制:  65536kB 描述 求两个大的正整数相减的差. 输入 共2行,第1行是被减数a,第2行是减数b(a > b).每个大整数不超过20 ...

  5. 2016.11.17 NOI plus day0

    今天很乱乱乱乱 根本不想写代码 玩了一早上了 昨晚失眠了 今天又懵逼了 中午就要走了 明天就要考试了 考完试回来就要补文化课了 现在我的内心很平静 因为已经紧张的冻结了 你知道什么叫彷徨么? 机房里的 ...

  6. Adobe Air移动开发本人体会

    采用FLASH BUILD4.6开发 1.没有mx:Canvas了,s:BordContainer未经手机优化,也不敢用,只有用s:Group 2.好多控件没有了,如DropDownList,Prog ...

  7. Quartz集群

    为什么选择Quartz: 1)资历够老,创立于1998年,比struts1还早,但是一直在更新(27 April 2012: Quartz 2.1.5 Released),文档齐全. 2)完全由Jav ...

  8. MMDrawerController的使用

    1.http://www.jianshu.com/p/9e55cbf7d5ab  MMDrawerController的使用

  9. iOS NSFileManager 使用详解

    使用NSFileManager 文件系统接口 允许访问文件夹内容 创建 重命名 删除文件 修改文件和文件属性,以及Finder对所有文件系统任务执行的一般操作. 访问NSFileManager,使用共 ...

  10. jquery noConflict详解

    noConflict是防止其他库也用了$作为全局变量而引起的冲突,我们看看jquery是怎么做的 首先jquery在代码的开始部分定义了2个私有变量: _jQuery = window.jQuery ...