请教在线文档的api

JavaScript028

请教在线文档的api,第1张

Android中文版

api手册地址:http://www.matools.com/api/android

Ant最新版

api手册地址:http://www.matools.com/api/ant

ASM字节码操作

api手册地址:http://www.matools.com/api/asm

Axis2最新版

api手册地址:http://www.matools.com/api/axis2

Bash脚本

api手册地址:http://www.matools.com/api/bash

Bootstrap 3

api手册地址:http://www.matools.com/api/bootstrap3

Bootstrap 4

api手册地址:http://www.matools.com/api/bootstrap4

C/C++

api手册地址:http://www.matools.com/api/c

C3P0连接池

api手册地址:http://www.matools.com/api/c3p0

CentOS使用文档

api手册地址:http://www.matools.com/api/centos

Commons-Beanutils

api手册地址:http://www.matools.com/api/commons-beanutils

Commons-Fileupload

api手册地址:http://www.matools.com/api/commons-fileupload

Commons-IO最新版

api手册地址:http://www.matools.com/api/commons-io

Commons-Lang最新版

api手册地址:http://www.matools.com/api/commons-long

Commons-Net最新版

api手册地址:http://www.matools.com/api/commons-net

CSS 3

api手册地址:http://www.matools.com/api/css

DBCP连接池

api手册地址:http://www.matools.com/api/dbcp

Dom4j

api手册地址:http://www.matools.com/api/dom4j

dubbo中文文档

api手册地址:http://www.matools.com/api/dubbo

EhCache

api手册地址:http://www.matools.com/api/ehcache

Freemarker

api手册地址:http://www.matools.com/api/freemarker

Go语言

api手册地址:http://www.matools.com/api/go

Hadoop

api手册地址:http://www.matools.com/api/hadoop

Hibernate中文版

api手册地址:http://www.matools.com/api/hibernate

IKAnalyzer中文版

api手册地址:http://www.matools.com/api/ikanalyzer

Java 10

api手册地址:http://www.matools.com/api/java10

Java 6

api手册地址:http://www.matools.com/api/java6

Java 7

api手册地址:http://www.matools.com/api/java7

Java 8中文版

api手册地址:http://www.matools.com/api/java8

jqGrid中文版

api手册地址:http://www.matools.com/api/jqgrid

Jquery中文版

api手册地址:http://www.matools.com/api/jquery

Json-lib

api手册地址:http://www.matools.com/api/json-lib

Junit4最新版

api手册地址:http://www.matools.com/api/junit

Kryo

api手册地址:http://www.matools.com/api/kryo

Log4j最新版

api手册地址:http://www.matools.com/api/log4j

Lucene

api手册地址:http://www.matools.com/api/lucene

Maven

api手册地址:http://www.matools.com/api/maven

Windows MFC中文版

api手册地址:http://www.matools.com/api/msdn

Mybatis

api手册地址:http://www.matools.com/api/mybatis

MySql中文版

api手册地址:http://www.matools.com/api/mysql

Netty 3.6

api手册地址:http://www.matools.com/api/netty

Nginx中文版

api手册地址:http://www.matools.com/api/nginx

OpenJPA最新版

api手册地址:http://www.matools.com/api/openjpa

PHP中文版

api手册地址:http://www.matools.com/api/php

POI-apache

api手册地址:http://www.matools.com/api/poi

QuickServer

api手册地址:http://www.matools.com/api/quickserver

redis中文参考文档

api手册地址:http://www.matools.com/api/redis

Ruby

api手册地址:http://www.matools.com/api/ruby

Ruby-library

api手册地址:http://www.matools.com/api/ruby-library

Ruby on Rails

api手册地址:http://www.matools.com/api/rubyonrails

Shiro

api手册地址:http://www.matools.com/api/shiro

Spring最新版

api手册地址:http://www.matools.com/api/spring

Spring for Android

api手册地址:http://www.matools.com/api/spring-android

Spring Boot

api手册地址:http://www.matools.com/api/spring-boot

Spring Cloud中文文档

api手册地址:http://www.matools.com/api/spring-cloud

Spring Security

api手册地址:http://www.matools.com/api/spring-security

Spring中文版

api手册地址:http://www.matools.com/api/spring-zh

Struts 2最新版

api手册地址:http://www.matools.com/api/struts2

Taperstry

api手册地址:http://www.matools.com/api/taperstry

TensorFlow中文

api手册地址:http://www.matools.com/api/tensorflow

Tomcat

api手册地址:http://www.matools.com/api/tomcat

Ubuntu

api手册地址:http://www.matools.com/api/ubuntu

Velocity 1.7

api手册地址:http://www.matools.com/api/velocity

VelocityTools2.0

api手册地址:http://www.matools.com/api/velocity-tools

Vue Router中文参考

api手册地址:http://www.matools.com/api/vue-router

vue.js中文文档

api手册地址:http://www.matools.com/api/vuejs

XMLBeans

api手册地址:http://www.matools.com/api/xmlbeans

Yahoo UI中文版

api手册地址:http://www.matools.com/api/yui

Zend Framework中文版

api手册地址:http://www.matools.com/api/zend-framework

Zookeeper

api手册地址:http://www.matools.com/api/zookeeper

主要原因:link type不支持js

浏览器支持

只有 rel 属性的 "stylesheet" 值得到了所有浏览器的支持。其他值只得到了部分地支持。

语法

<link rel="value">

属性值

值 描述

alternate 文档的替代版本(比如打印页、翻译或镜像)。

stylesheet 文档的外部样式表。

start 集合中的第一个文档。

next 集合中的下一个文档。

prev 集合中的上一个文档。

contents 文档的目录。

index 文档的索引。

glossary 在文档中使用的词汇的术语表(解释)。

copyright 包含版权信息的文档。

chapter 文档的章。

section 文档的节。

subsection 文档的小节。

appendix 文档的附录。

help 帮助文档。

bookmark 相关文档。

1.1 SwaggerUI

SwaggerUI 是一个简单的Restful API 测试和文档工具。简单、漂亮、易用(官方demo)。通过读取JSON 配置显示API. 项目本身仅仅也只依赖一些 html,css.js静态文件. 你可以几乎放在任何Web容器上使用。

1.2 Swashbuckle

Swashbuckle 是.NET类库,可以将WebAPI所有开放的控制器方法生成对应SwaggerUI的JSON配置。再通过SwaggerUI 显示出来。类库中已经包含SwaggerUI 。所以不需要额外安装。

2.快速开始

创建项目 OnlineAPI来封装百度音乐服务(示例下载) ,通过API可以搜索、获取音乐的信息和播放连接。

我尽量删除一些我们demo中不会用到的一些文件,使其看上去比较简洁。

WebAPI 安装 Swashbuckle

Install-Package Swashbuckle

代码注释生成文档说明。

Swashbuckle 是通过生成的XML文件来读取注释的,生成 SwaggerUI,JSON 配置中的说明的。

安装时会在项目目录 App_Start 文件夹下生成一个 SwaggerConfig.cs 配置文件,用于配置 SwaggerUI 相关展示行为的。如图:

将配置文件大概99行注释去掉并修改为

c.IncludeXmlComments(GetXmlCommentsPath(thisAssembly.GetName().Name))

并在当前类中添加一个方法

/// <summary>

/// </summary>

/// <param name="name"></param>

/// <returns></returns>

protected static string GetXmlCommentsPath(string name)

{

return string.Format(@"{0}\bin\{1}.XML", AppDomain.CurrentDomain.BaseDirectory, name)

}

紧接着你在此Web项目属性生成选卡中勾选 “XML 文档文件”,编译过程中生成类库的注释文件

添加百度音乐 3个API

访问 http://<youhost>/swagger/ui/index,最终显示效果

我们通过API 测试API 是否成功运行

3.添加自定义HTTP Header

在开发移动端 API时常常需要验证权限,验证参数放在Http请求头中是再好不过了。WebAPI配合过滤器验证权限即可

首先我们需要创建一个 IOperationFilter 接口的类。IOperationFilter

using System

using System.Collections.Generic

using System.Linq

using System.Web

using System.Web.Http

using System.Web.Http.Description

using System.Web.Http.Filters

using Swashbuckle.Swagger

namespace OnlineAPI.Utility

{

public class HttpHeaderFilter : IOperationFilter

{

public void Apply(Operation operation, SchemaRegistry

schemaRegistry, ApiDescription apiDescription)

{

if (operation.parameters == null) operation.parameters = new

List<Parameter>()

var filterPipeline =

apiDescription.ActionDescriptor.GetFilterPipeline()

//判断是否添加权限过滤器

var isAuthorized = filterPipeline.Select(filterInfo =>

filterInfo.Instance).Any(filter =>filter is IAuthorizationFilter)

//判断是否允许匿名方法

var allowAnonymous =

apiDescription.ActionDescriptor.GetCustomAttributes<AllowAnonymousAttribute>().Any()

if (isAuthorized &&!allowAnonymous)

{

operation.parameters.Add(new Parameter

{

name = "access-key",

@in = "header",

description = "用户访问Key",

required = false,

type = "string"

})

}

}

}

}

在 SwaggerConfig.cs 的 EnableSwagger 配置匿名方法类添加一行注册代码

c.OperationFilter<HttpHeaderFilter>()

添加Web权限过滤器

using System

using System.Collections.Generic

using System.Linq

using System.Net

using System.Net.Http

using System.Text

using System.Web

using System.Web.Http

using System.Web.Http.Controllers

using Newtonsoft.Json

namespace OnlineAPI.Utility

{

/// <summary>

///

/// </summary>

public class AccessKeyAttribute : AuthorizeAttribute

{

/// <summary>

/// 权限验证

/// </summary>

/// <param name="actionContext"></param>

/// <returns></returns>

protected override bool IsAuthorized(HttpActionContext actionContext)

{

var request = actionContext.Request

if (request.Headers.Contains("access-key"))

{

var accessKey = request.Headers.GetValues("access-key").SingleOrDefault()

//TODO 验证Key

return accessKey == "123456789"

}

return false

}

/// <summary>

/// 处理未授权的请求

/// </summary>

/// <param name="actionContext"></param>

protected override void HandleUnauthorizedRequest(HttpActionContext actionContext)

{

var content = JsonConvert.SerializeObject(new {State = HttpStatusCode.Unauthorized})

actionContext.Response = new HttpResponseMessage

{

Content = new StringContent(content, Encoding.UTF8, "application/json"),

StatusCode = HttpStatusCode.Unauthorized

}

}

}

}

在你想要的ApiController 或者是 Action 添加过滤器

[AccessKey]

最终显示效果

4.显示上传文件参数

SwaggerUI 有上传文件的功能和添加自定义HTTP Header 做法类似,只是我们通过特殊的设置来标示API具有上传文件的功能

using System

using System.Collections.Generic

using System.Linq

using System.Web

using System.Web.Http.Description

using Swashbuckle.Swagger

namespace OnlineAPI.Utility

{

/// <summary>

///

/// </summary>

public class UploadFilter : IOperationFilter

{

/// <summary>

/// 文件上传

/// </summary>

/// <param name="operation"></param>

/// <param name="schemaRegistry"></param>

/// <param name="apiDescription"></param>

public void Apply(Operation operation, SchemaRegistry schemaRegistry, ApiDescription apiDescription)

{

if (!string.IsNullOrWhiteSpace(operation.summary) &&operation.summary.Contains("upload"))

{

operation.consumes.Add("application/form-data")

operation.parameters.Add(new Parameter

{

name = "file",

@in = "formData",

required = true,

type = "file"

})

}

}

}

}

在 SwaggerConfig.cs 的 EnableSwagger 配置匿名方法类添加一行注册代码

c.OperationFilter<UploadFilter>()

API 文档展示效果