博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Asp.net Web Api开发(第四篇)Help Page配置和扩展
阅读量:7046 次
发布时间:2019-06-28

本文共 3712 字,大约阅读时间需要 12 分钟。

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

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

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

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

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

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

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

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

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

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

 

[csharp]   
 
 
  1. using System;  
  2. using System.Linq;  
  3. using System.Reflection;  
  4. using System.Web.Http.Controllers;  
  5. using System.Web.Http.Description;  
  6. using WebApplication2.Areas.HelpPage.ModelDescriptions;  
  7.   
  8. namespace WebApplication2.Areas.HelpPage.App_Start  
  9. {  
  10.     public class MultiXmlDocumentationProvider : IDocumentationProvider, IModelDocumentationProvider  
  11.     {  
  12.         private readonly XmlDocumentationProvider[] Providers;  
  13.         public MultiXmlDocumentationProvider(params string[] paths)  
  14.         {  
  15.             this.Providers = paths.Select(p => new XmlDocumentationProvider(p)).ToArray();  
  16.         }  
  17.   
  18.         public string GetDocumentation(MemberInfo subject)  
  19.         {  
  20.             return this.GetFirstMatch(p => p.GetDocumentation(subject));  
  21.         }  
  22.   
  23.         public string GetDocumentation(Type subject)  
  24.         {  
  25.             return this.GetFirstMatch(p => p.GetDocumentation(subject));  
  26.         }  
  27.   
  28.         public string GetDocumentation(HttpControllerDescriptor subject)  
  29.         {  
  30.             return this.GetFirstMatch(p => p.GetDocumentation(subject));  
  31.         }  
  32.   
  33.         public string GetDocumentation(HttpActionDescriptor subject)  
  34.         {  
  35.             return this.GetFirstMatch(p => p.GetDocumentation(subject));  
  36.         }  
  37.   
  38.         public string GetDocumentation(HttpParameterDescriptor subject)  
  39.         {  
  40.             return this.GetFirstMatch(p => p.GetDocumentation(subject));  
  41.         }  
  42.   
  43.         public string GetResponseDocumentation(HttpActionDescriptor subject)  
  44.         {  
  45.             return this.GetFirstMatch(p => p.GetDocumentation(subject));  
  46.         }  
  47.   
  48.         private string GetFirstMatch(Func<XmlDocumentationProvider, string> expr)  
  49.         {  
  50.             return this.Providers  
  51.                 .Select(expr)  
  52.                 .FirstOrDefault(p => !String.IsNullOrWhiteSpace(p));  
  53.         }  
  54.     }  
  55. }  

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

 

 

[csharp]   
 
 
  1. using System.Diagnostics.CodeAnalysis;  
  2. using System.Web;  
  3. using System.Web.Http;  
  4. using WebApplication2.Areas.HelpPage.App_Start;  
  5.   
  6. namespace WebApplication2.Areas.HelpPage  
  7. {  
  8.     public static class HelpPageConfig  
  9.     {  
  10.         [SuppressMessage("Microsoft.Globalization", "CA1303:Do not pass literals as localized parameters",  
  11.             MessageId = "WebApplication2.Areas.HelpPage.TextSample.#ctor(System.String)",  
  12.             Justification = "End users may choose to merge this string with existing localized resources.")]  
  13.         [SuppressMessage("Microsoft.Naming", "CA2204:Literals should be spelled correctly",  
  14.             MessageId = "bsonspec",  
  15.             Justification = "Part of a URI.")]  
  16.         public static void Register(HttpConfiguration config)  
  17.         {  
  18.             config.SetDocumentationProvider(new MultiXmlDocumentationProvider(  
  19.                 HttpContext.Current.Server.MapPath("~/bin/WebApplication2.XML")));  
  20.         }  
  21.     }  
  22. }  

 

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

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

 

[csharp]   
 
 
  1. using System.Web.Http;  
  2.   
  3. namespace WebApplication2.Controllers  
  4. {  
  5.     /// <summary>  
  6.     /// 测试响应对象  
  7.     /// </summary>  
  8.     public struct TestResponse {  
  9.         /// <summary>  
  10.         /// 姓名  
  11.         /// </summary>  
  12.        public string Name;  
  13.         /// <summary>  
  14.         /// 年龄  
  15.         /// </summary>  
  16.         public int Age;  
  17.     }  
  18.   
  19.     /// <summary>  
  20.     /// 测试  
  21.     /// </summary>  
  22.     public class TestController : ApiController  
  23.     {  
  24.         /// <summary>  
  25.         /// 测试接口  
  26.         /// </summary>  
  27.         /// <returns></returns>  
  28.         [HttpPost]  
  29.         [Route("api/300/1000")]  
  30.         public TestResponse JustTest()  
  31.         {  
  32.             return new TestResponse() { Name = "测试员", Age = 26 };  
  33.         }  
  34.     }  
  35. }  

 

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

 

[csharp]   
 
 
  1. using System.Web.Http;  
  2. using System.Web.Mvc;  
  3.   
  4. namespace WebApplication2  
  5. {  
  6.     public class WebApiApplication : System.Web.HttpApplication  
  7.     {  
  8.         protected void Application_Start()  
  9.         {  
  10.             AreaRegistration.RegisterAllAreas();  
  11.             GlobalConfiguration.Configure(WebApiConfig.Register);  
  12.         }  
  13.     }  
  14. }  

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

 

你可能感兴趣的文章
一步一步写一个简单通用的makefile(二)
查看>>
sunspot使用
查看>>
Zombie.js Insanely fast, headless full-stack testing using Node.js
查看>>
POJ2406-Power Strings(kmp循环节)
查看>>
BCM路由全智能固件升级软件tftp,一键刷路由及常用固件下载
查看>>
个人认识:直接断电和发送复位信号给主板有啥区别?
查看>>
测试体会:WAYOS新架构(即二代QOS)的新功能解释
查看>>
UVA 10169 Urn-ball Probabilities !
查看>>
每日一例,练就编程高手
查看>>
no argument specified with option "/LIBPATH:"错误的解决【转载】
查看>>
初涉c#设计模式-Factory Pattern
查看>>
JRuby——Java和Ruby的强强联合
查看>>
ipcs和ipcrm用法简介
查看>>
[Go 笔记]关于 Panic和 Recover
查看>>
关于HP Diagnostics
查看>>
Oracle中的二进制、八进制、十进制、十六进制相互转换函数
查看>>
关于empty函数的输出
查看>>
SCI
查看>>
【菜鸟学习Linux】-第三章- Linux环境搭建-使用VMware9安装Ubuntu 12.04系统
查看>>
分享一组Rpg Marker人物行走,游戏素材图片,共20张图片
查看>>