为了方面APP开发人员,服务端的接口都应当提供详尽的API说明。但每次有修改,既要维护代码,又要维护文档,一旦开发进度紧张,很容易导致代码与文档不一致。

Web API有一个Help Page插件,可以很方便的根据代码及注释自动生成相关API说明页面。

Help Page安装步骤及扩展(以VS2015为例):

右键点击WebAPI项目的引用,选择"管理NuGet程序包"

在搜索框中输入 helppage进行搜索,结果如下图:

然后在右侧边栏点击安装按钮即可进行插件安装了。

安装完成后,你会发现项目下多了不少文件:

接下来,我们对Areas/HelpPage/App_Start/HelpPageConfig.cs进行改造。

改造前,我们需要先了解下HelpPageConfig.cs,其中的Register方法是用于注册Help Page页面需要展示的API的文档的。默认情况下,该方法只支持单个文档导入,所以我们需要扩展下。

我们创建一个可多文件注册的类:

using System;

using System.Linq;
using System.Reflection;

using System.Web.Http.Controllers;

using System.Web.Http.Description;

using WebApplication2.Areas.HelpPage.ModelDescriptions;

namespace WebApplication2.Areas.HelpPage.App_Start

{

public class MultiXmlDocumentationProvider : IDocumentationProvider, IModelDocumentationProvider

{

private readonly XmlDocumentationProvider[] Providers;

public MultiXmlDocumentationProvider(params string[] paths)

{

this.Providers = paths.Select(p => new XmlDocumentationProvider(p)).ToArray();

}

public string GetDocumentation(MemberInfo subject)

{

return this.GetFirstMatch(p => p.GetDocumentation(subject));

}

public string GetDocumentation(Type subject)

{

return this.GetFirstMatch(p => p.GetDocumentation(subject));

}

public string GetDocumentation(HttpControllerDescriptor subject)

{

return this.GetFirstMatch(p => p.GetDocumentation(subject));

}

public string GetDocumentation(HttpActionDescriptor subject)

{

return this.GetFirstMatch(p => p.GetDocumentation(subject));

}

public string GetDocumentation(HttpParameterDescriptor subject)

{

return this.GetFirstMatch(p => p.GetDocumentation(subject));

}

public string GetResponseDocumentation(HttpActionDescriptor subject)

{

return this.GetFirstMatch(p => p.GetDocumentation(subject));

}

private string GetFirstMatch(Func<XmlDocumentationProvider, string> expr)

{

return this.Providers

.Select(expr)

.FirstOrDefault(p => !String.IsNullOrWhiteSpace(p));

}

}

}

然后重写HelpPageConfig.cs文件中的代码如下:

using System.Diagnostics.CodeAnalysis;

using System.Web;
using System.Web.Http;

using WebApplication2.Areas.HelpPage.App_Start;

namespace WebApplication2.Areas.HelpPage

{

public static class HelpPageConfig

{

[SuppressMessage("Microsoft.Globalization", "CA1303:Do not pass literals as localized parameters",

MessageId = "WebApplication2.Areas.HelpPage.TextSample.#ctor(System.String)",

Justification = "End users may choose to merge this string with existing localized resources.")]

[SuppressMessage("Microsoft.Naming", "CA2204:Literals should be spelled correctly",

MessageId = "bsonspec",

Justification = "Part of a URI.")]

public static void Register(HttpConfiguration config)

{

config.SetDocumentationProvider(new MultiXmlDocumentationProvider(

HttpContext.Current.Server.MapPath("~/bin/WebApplication2.XML")));

}

}

}

这里要注意下WebApplication2.XML,这个文件是需要我们对相关项目属性进行设置下的,让其生成相关xml文件。

然后我们来创建一个Controller用于测试。

using System.Web.Http;

namespace WebApplication2.Controllers
{
/// <summary>
/// 测试响应对象
/// </summary>
public struct TestResponse {
/// <summary>
/// 姓名
/// </summary>
public string Name;
/// <summary>
/// 年龄
/// </summary>
public int Age;
}
/// <summary>
/// 测试
/// </summary>
public class TestController : ApiController
{
/// <summary>
/// 测试接口
/// </summary>
/// <returns></returns>
[HttpPost]
[Route("api/300/1000")]
public TestResponse JustTest()
{
return new TestResponse() { Name = "测试员", Age = 26 };
}
}
}

因为创建的是Web API项目,所以这里还要修改下Global.asax,注册Area。

using System.Web.Http;

using System.Web.Mvc;
namespace WebApplication2

{

public class WebApiApplication : System.Web.HttpApplication

{

protected void Application_Start()

{

AreaRegistration.RegisterAllAreas();

GlobalConfiguration.Configure(WebApiConfig.Register);

}

}

}

接下来,编译运行调试起来,效果如下图。

 

Asp.net Web Api开发Help Page配置和扩展的更多相关文章

  1. Asp.net Web Api开发Help Page 添加对数据模型生成注释的配置和扩展

    在使用webapi框架进行接口开发的时候,编写文档会需要与接口同步更新,如果采用手动式的更新的话效率会非常低.webapi框架下提供了一种自动生成文档的help Page页的功能. 但是原始版本的效果 ...

  2. Asp.Net Web API开发微信后台

    如果说用Asp.Net开发微信后台是非主流,那么Asp.Net Web API的微信后台绝对是不走寻常路. 需要说明的是,本人认为Asp.Net Web API在开发很多不同的请求方法的Restful ...

  3. 水果项目第3集-asp.net web api开发入门

    app后台开发,可以用asp.net webservice技术. 也有一种重量级一点的叫WCF,也可以用来做app后台开发. 现在可以用asp.net web api来开发app后台. Asp.net ...

  4. ASP.NET Web API 2 之路由配置

    Ø  简介 ASP.NET Web API 路由配置也是必须掌握的技术点之一,要真正的完全掌握和理解它也是需要一定的过程的.不过,在平常的开发过程中,对它有基本的了解就足够了.因为我们主要关注点并不在 ...

  5. [目录]ASP.NET web api开发实战

    第一章:Restful web service v.s. RPC style web service 第二章:ASP.NET web api v.s. WCF v.s. ASP.NET web ser ...

  6. 创建 ASP.NET Web API的Help Page

    转:创建WEBAPI项目 转:添加测试API中的ASP.NET Web API帮助页面

  7. Asp.net Web Api开发(第四篇)Help Page配置和扩展

    https://blog.csdn.net/sqqyq/article/details/52708613

  8. asp.net web api 开发时应当注意的事项

    Self referencing when returning chain of objects. This can be solved using a design pattern called t ...

  9. Creating Help Pages for ASP.NET Web API -摘自网络

    When you create a web API, it is often useful to create a help page, so that other developers will k ...

随机推荐

  1. AnnotationConfigBeanDefinitionParser are only available on JDK 1.5 and higher

    報錯: org.springframework.beans.factory.BeanDefinitionStoreException: Unexpected exception parsing XML ...

  2. Java马士兵高并发编程视频学习笔记(一)

    1.同一个资源,同步和非同步的方法可以同时调用 package com.dingyu; public class Y { public synchronized void m1() { System. ...

  3. 详解javascript事件绑定使用方法

    由于html是从上至下加载的,通常我们如果在head部分引入javascript文件,那么我们都会在javascript的开头添加window.onload事件,防止在文档问加载完成时进行DOM操作所 ...

  4. 初学HTML-1

    HTML:Hyper Text Markup Language的缩写    超文本标记语言,用来描述文本的语义,这些文本———超文本,也叫标签. 基本格式: <html> <head ...

  5. vue项目编辑修改时批量回显数据

    selectCityServiceOne() { let sendData = { token: this.token, id: this.id } post_ajax('backStage/city ...

  6. Ubuntu、deepin 支持 yum

    1,首先检测是否安装了build-essential程序包 sudo apt-get install build-essential 2,安装 yum sudo apt-get yum 3,检测是否安 ...

  7. SpringBoot集成Swagger接口管理工具

    手写Api文档的几个痛点: 文档需要更新的时候,需要再次发送一份给前端,也就是文档更新交流不及时. 接口返回结果不明确 不能直接在线测试接口,通常需要使用工具,比如postman 接口文档太多,不好管 ...

  8. (网页)习惯了CS回车操作人员,操作BS网页表单也是回车666

    1.第一步把表单,里面需要回车的input,或者是其他的表单按钮给一个clsss,例如下面的$('.cls'); 2.第二步,  把下面的代码复制过去,填写完最后一个自动提交:$("#sav ...

  9. 使用Visual Studio Team Services敏捷规划和项目组合管理(二)——VSTS中的工作项

    使用Visual Studio Team Services敏捷规划和项目组合管理(二)--VSTS中的工作项 1.通过project/team下拉菜单选择MyHealthClinic\Web,导航到W ...

  10. Essential pro angular and asp.net core 笔记

    1. dotnet ef相关命令 删除数据库(适合只有一个数据库的情形) dotnet ef database drop --force 更新数据库(适合只有一个数据库的情形) dotnet ef d ...