为ASP.NET Web API创建帮助页面-Help Page

前言

创建Web API时,创建帮助页面通常很有用,以便其他开发人员知道如何调用您的API。您可以手动创建所有文档,但是最好自动生成。为了简化此任务,ASP.NET Web API提供了一个库,用于在运行时自动生成帮助页面。

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

实现

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

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

 

 

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

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

 

3、增加MultiXmlDocumentationProvider 文件

using System;using System.Collections.Generic;using System.Linq;using System.Reflection;using System.Web;using System.Web.Http.Controllers;using System.Web.Http.Description;using WebApplication41.Areas.HelpPage.ModelDescriptions;
namespace WebApplication41.Areas.HelpPage{    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));        }    }}

   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/WebApplication41.XML")));        }    }

4、重写HelpPageConfig

5、然后你会发现一切都是英文,那么怎么显示中文说明呢

首先 选中项目,右键-属性-生成 选择下面的XML文档文件,目录填写如下图所示

 

 

 

 

6、下一步安装TestAPI的插件,这个插件会在页面生成一个按钮,点击这个按钮可以直接调试WEBAPI 方便到爆炸。

打开Nuget包管理器的控制台 输入  Install-Package WebApiTestClient  按成安装后多了这两个文件

 

 

 

 

 

 

大功告成

 

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值