zoukankan      html  css  js  c++  java
  • 为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  按成安装后多了这两个文件

    大功告成

  • 相关阅读:
    C++中用Int转成bool时,只有0是false,其他都是true。这个和其他语言很不一样,注意不要掉坑里了。
    C# 获取动态验证码?
    Silverlight单元格事件
    LDAPHelper
    Perl内部保留变量(系统变量)
    WebSphere MQ基础命令
    老鼠, 老虎傻傻分不清楚之Double.NaN
    TextBlock or Label?
    如何阅读代码
    EDID
  • 原文地址:https://www.cnblogs.com/lyl6796910/p/14030594.html
Copyright © 2011-2022 走看看