Web Api 2 接口API文档美化

使用用第三方提供的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文件中的配置保持一样。

时间: 2024-08-01 22:46:54

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

利用iStylePDF的API实现在PDF文档中动态插入一幅图片

PDF的交互特性里面有一种叫Annotation的注释和标记对象,我们可以在一个注释对象中放入自己想要的数据.在这篇文章中所讲到的插入一幅图片,是我们在PDF应用中经常需要这样做的,比如个人签名的图片等. 首先我们来认识下PDF里面中的Annotations是何东东.一个annotation关联了一些注释.声音.电影等对象,PDF标准中预定义了一些常用的注释类型.在我们的帮助文档中有详细的说明,等下也会用到的,我列举出来了,如下所示 名称 数值 描述 spAnnotText 0 文本 spAnn

SpringBoot2.0系列教程(六)Springboot框架添加Swagger2来在线自动生成接口的文档+测试功能

Hello大家好,本章我们添加Swagger2来在线自动生成接口的文档+测试功能.有问题可以联系我[email protected].另求各路大神指点,感谢 一:什么是Swagger Swagger是一款通过我们添加的注解来对方法进行说明,来自动生成项目的在线api接口文档的web服务. 二:添加Swagger2依赖 <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swag

Java之------socket查看指定URL的Web页编辑器及HTML文档

本例只能用于查看比较简单的网页,像是现在jsp开发的网页有很多地方时无法显示的,但这种程序还是值得学习一下的 package cn.hncu.url; import java.util.Date; import java.text.SimpleDateFormat; import java.awt.event.*; import javax.swing.*; import javax.swing.event.*; import java.io.*; import java.net.*; publ

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

原文:http://blog.csdn.net/m13666368773/article/details/6888513/ 支付宝接口使用文档说明 支付宝异步通知(notify_url)与return_url. 现支付宝的通知有两类. 1-服务器通知(支付宝通知我们的服务器),对应的参数为notify_url,支付宝通知使用POST方式 2-页面跳转通知(支付成功后,从支付宝跳转到指定的地址),对应的参数为return_url,支付宝通知使用GET方式 (通知地址不需要像以前一样去账户内设置,而

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

https://yq.aliyun.com/ziliao/1768?spm=5176.8246799.blogcont.24.1PxYoX 摘要: 本文讲的是Java实现web在线预览office文档与pdf文档实例, 1.首先我们需要找到可以把office转换成pdf的方法,查找资料发现有openoffice这一软件可以把office转换成pdf,这一软件先下载下来,然后记住自己安装的在那个位置.然后在cmd环境下进入安装目录的program目 云计算 云服务器ECS 大数据 建站 备案 文档

支付宝接口使用文档说明 支付宝异步通知

支付宝接口使用文档说明 支付宝异步通知(notify_url)与return_url. 现支付宝的通知有两类. A服务器通知,对应的参数为notify_url,支付宝通知使用POST方式 B页面跳转通知,对应的参数为return_url,支付宝通知使用GET方式 (通知地址不需要像以前一样去账户内设置,而是由客户在支付的时候通过参数传递给我地址. 例如 notify_url=http://www.xxx.com/notify_alipay.asp 注意:www.XXX.com是您网站的域名,也可

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

使用第三方提供的swgger ui 可有效提高 web api 接口列表的阅读性,并且可以在页面中测试服务接口. 但本人在查阅大量资料并进行编码测试后,发现大部分的swagger实例并不能有效运行.例如如下两个网址:http://www.cnblogs.com/caodaiming/p/4156476.html 和 http://bitoftech.net/2014/08/25/asp-net-web-api-documentation-using-swagger/.经过本人的一番折腾,最终发现

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

之前在项目中有用到webapi对外提供接口,发现在项目中有根据webapi的方法和注释自动生成帮助文档,还可以测试webapi方法,功能很是强大,现拿出来与大家分享一下. 先看一下生成的webapi文档. 1.下图展示的是生成帮助文档首页面,其中Values是controller,API下面的列表展示出请求的http方法(Get,POST等),请求的action,方法的描述. 2.点击红框内的链接,打开api方法的详情页面,如下图所示, 3.点击Test API打开如下页面 4.输入参数,点击S

Web Api 自动生成帮助文档

新建Web Api项目之后,会在首页有API的导航菜单,点击即可看到API帮助文档,不过很遗憾,Description 是没有内容的. 怎么办呢? 第一步: 如果用VS2013 新建项目的(VS2012没试过),项目中会有 Areas/HelpPage 这样的目录,你没看错,文档就是这货生成的. 如果要是删除了或者,没有这个目录怎么办呢?没关系,你只需要使用NuGet添加  [Microsoft.AspNet.WebApi.HelpPage]这货,然后你就发现,你的项目自动添加了 Areas/H