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


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

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

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

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

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

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

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

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

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

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

    [csharp] view plain copy
     
     print?
    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] view plain copy
     
     print?
    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] view plain copy
     
     print?
    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] view plain copy
     
     print?
    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. }  

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

  • 相关阅读:
    SpringBoot异步方法优化处理提高响应速度
    sysbench性能测试
    lightdb停不掉FATAL: the database system is shutting down
    LOG: invalid primary checkpoint record/PANIC: could not locate a valid checkpoint record恢复方法
    postgresql/lightdb无法停止一例
    Program terminated with signal 6, Aborted
    PostgreSQL/lightdb逻辑复制详解
    LightDB Enterprise Postgres 22.1正式发布原生分布式版本
    postgresql/lightdb逻辑备份、恢复最佳实践
    Doris 与 ClickHouse 的深度对比及选型建议 徐正柱
  • 原文地址:https://www.cnblogs.com/zxtceq/p/6903231.html
Copyright © 2020-2023  润新知