ASP的Web API帮助页面 - 链接到从XML&LT类;见>标签 [英] ASP Web API Help pages - Link to class from XML <see> tag

查看:208
本文介绍了ASP的Web API帮助页面 - 链接到从XML&LT类;见>标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发Web的API项目,我是微软的HelpPages与自动生成的文档pssed很IM $ P $。

我使用的官方网站<启用自定义文件href=\"http://www.asp.net/web-api/overview/getting-started-with-aspnet-web-api/creating-api-help-pages\"相对=nofollow>创建帮助页面

成功生成的文档,但没有引用从类的&LT;见CREF =&GT; 标签似乎被添加到描述,HelpPages简单地忽略它们(这是有原因的)。

我真的很想有此功能在我的项目,我搜索了很多(亲近有时),但没有给出一个令人信服的答案。

这就是为什么我决定把我的解决方案张贴这样的修改,希望有益于其他程序员和免除他们一些时间和精力。
(我的答案是在下面的回复)


解决方案

我的解决方案如下:


  1. 您已经有了自定义文件(从生成的XML文件)工作

  2. 启用文档中HTML和XML标签,他们通常会得到过滤掉,谢谢这个帖子可以preserve他们。结果
    只需访问:项目名>领域> HelpPage> XmlDocumentationProvider.cs结果
    在方法行123:GetTagValue(XPathNavigator的parentNode,字符串标记名)结果
  3. ;改变code 返回node.Value.Trim(); 返回node.InnerXml >
  4. 创建以下的部分观点:结果
    项目名\\区\\ HelpPage \\意见\\帮助** _ ** XML_SeeTagsRenderer.cshtml结果
    这是我的code:


  @using System.Web.Http;
@using MyProject.Areas.HelpPage.Controllers;
@using MyProject.Areas.HelpPage;
@using MyProject.Areas.HelpPage.ModelDescriptions
@using System.Text.RegularEx pressions
@model串


  @ {
    INT @index = 0;
    字符串@xml =模型;
    如果(@xml == NULL)
        @xml =;
    正则表达式@seeRegex ​​=新的正则表达式(&LT;(*)见(+)CREF = \\([^ \\])([^ \\] +)\\(*)/&gt;中); //正则表达式(&LT;见CREF = \\T:([^ \\] +)\\/&gt;中);
    匹配@xmlSee = @ seeRegex.Match(@xml);
    字符串@typeAsText =;
    键入@tp;    ModelDescriptionGenerator modelDescriptionGenerator =(新HelpController())Configuration.GetModelDescriptionGenerator();}@if(XML =与&amp;!&安培; xmlSee = NULL&放大器;!&安培; xmlSee.Length大于0)
{    而(xmlSee = NULL&放大器;!&安培; xmlSee.Length大于0)
    {            @ MvcHtmlString.Create(@ xml.Substring(@index,@ xmlSee.Index - @index))        INT startingIndex = xmlSee.Value.IndexOf(':')+ 1;
        INT endIndex的= xmlSee.Value.IndexOf('',startingIndex);
        typeAsText = xmlSee.Value.Substring(startingIndex,endIndex的 - startingIndex); //.Replace(\"<see CREF = \\T,).Replace(\\/&gt;中,);
        System.Reflection.Assembly ThisAssembly = typeof运算(ThisProject.Controllers.HomeController).Assembly;
        TP = ThisAssembly.GetType(@typeAsText);        如果(TP == NULL)//尝试另一个引用的项目
        {
            System.Reflection.Assembly externalAssembly = typeof运算(MyExternalReferncedProject.AnyClassInIt).Assembly;
            TP = externalAssembly.GetType(@typeAsText);
        }        如果需要(TP == NULL)//另一种参考项目 -
        {
            System.Reflection.Assembly anotherExtAssembly = typeof运算(MyExternalReferncedProject2.AnyClassInIt).Assembly;
            TP = anotherExtAssembly .GetType(@typeAsText);
        }
        如果(TP == NULL)//嵌套类的情况下,
        {
            System.Reflection.Assembly thisAssembly = typeof运算(ThisProject.Controllers.HomeController).Assembly;
            //下面的code做是为了支持检测嵌套类。
            VAR processedTypeString = typeAsText;
            变种lastIndexofPoint = typeAsText.LastIndexOf('。');
            而(lastIndexofPoint大于0和放大器;&放大器; TP == NULL)
            {
                。processedTypeString = processedTypeString.Insert(lastIndexofPoint,+),删除(lastIndexofPoint + 1,1);
                TP = SPLocatorBLLAssembly.GetType(processedTypeString); //嵌套类是公认的:namespace.outerClass + nestedClass
                lastIndexofPoint = processedTypeString.LastIndexOf('。');
            }
        }        如果(@tp!= NULL)
        {
            ModelDescription MD = modelDescriptionGenerator.GetOrCreateModelDescription(TP);
                @ Html.DisplayFor(M = GT; md.ModelType,ModelDescriptionLink,新的{modelDescription = MD})
        }
        其他
        {
                @ MvcHtmlString.Create(@typeAsText)
        }
        指数= xmlSee.Index + xmlSee.Length;
        xmlSee = xmlSee.NextMatch();
    }
            @ MvcHtmlString.Create(@ xml.Substring(@index,@ xml.Length - @index))
}
其他
{
        @ MvcHtmlString.Create(@xml);
}

<醇开始=4>

  • 最后转到:项目名\\区\\ HelpPage \\意见\\帮助\\ DisplayTemplates ** ** Parameters.cshtml结果
    在行'20',我们有相应的文档中描述的code。结果
    替换此:

     &LT; TD class =参数的文档&GT;
                    &所述p为H.;
                        @ parameter.Documentation
                    &所述; / P&GT;
                &LT; / TD&GT;


  • 与此:

     &LT; TD class =参数的文档&GT;
                        &所述p为H.;
                            @ Html.Partial(_ XML_SeeTagsRenderer(@ parameter.Documentation == NULL:@ parameter.Documentation.ToString()))
                        &所述; / P&GT;
                    &LT; / TD&GT;

    &安培;瞧,你必须有它现在的工作。结果
    注:


    • 我试图把HTML列表的文档里面,它呈现的是精细

    • 我想多类引用(多个&LT;见CREF =MyClass的&GT; 和我正常工作

    • 您不能引用这是一个类
    • 内声明的类
    • 当你指的是一类就是当前项目以外,请添加项目中的类的装配.getType(检查我上面的code)

    • 内发现任何未找到类&LT;见CREF&GT; 将它在描述印刷(例如,如果您引用的属性或命名空间,全名code将不会将其识别为一个类型/类,但它会被打印)

    I'm working on Developing a Web-API project, and i'm very impressed with the auto generated documentation by Microsoft's HelpPages.

    i enabled custom documentation using the official site creating Help Pages

    the documentation is generated successfully BUT none of the references to Classes from the <See cref=""> Tag seems to be added to the description, the HelpPages Simply ignores them (that's for a reason).

    i really wanted to have this feature in my project, i searched a lot (got close sometimes) but none gave a convincing answer.

    That's why i decided to post my solution to this tweak and hopefully benefit other programmers and spare them some time and effort. (my answer is in the replies below)

    解决方案

    my solution is the following:

    1. you've got your custom documentation (from the generated xml file) working
    2. enable HTML and XML tags within the documentation, they normally get filtered out, thanks this Post you can preserve them.
      simply go to: ProjectName > Areas > HelpPage > XmlDocumentationProvider.cs
      on line 123 in method: GetTagValue(XPathNavigator parentNode, string tagName)
      change the code return node.Value.Trim(); to return node.InnerXml;
    3. create the following partial view:
      ProjectName\Areas\HelpPage\Views\Help**_XML_SeeTagsRenderer.cshtml**
      this is my code:

    @using System.Web.Http;
    @using MyProject.Areas.HelpPage.Controllers;
    @using MyProject.Areas.HelpPage;
    @using MyProject.Areas.HelpPage.ModelDescriptions
    @using System.Text.RegularExpressions
    @model string
    

    @{
        int @index = 0;
        string @xml = Model;
        if (@xml == null)
            @xml = "";
        Regex @seeRegex = new Regex("<( *)see( +)cref=\"([^\"]):([^\"]+)\"( *)/>");//Regex("<see cref=\"T:([^\"]+)\" />");
        Match @xmlSee = @seeRegex.Match(@xml);
        string @typeAsText = "";
        Type @tp;
    
        ModelDescriptionGenerator modelDescriptionGenerator = (new HelpController()).Configuration.GetModelDescriptionGenerator();
    
    }
    
    @if (xml !="" && xmlSee != null && xmlSee.Length > 0)
    {
    
        while (xmlSee != null && xmlSee.Length > 0)
        {
    
                @MvcHtmlString.Create(@xml.Substring(@index, @xmlSee.Index - @index))
    
            int startingIndex = xmlSee.Value.IndexOf(':')+1;
            int endIndex = xmlSee.Value.IndexOf('"', startingIndex);
            typeAsText = xmlSee.Value.Substring(startingIndex, endIndex - startingIndex);  //.Replace("<see cref=\"T:", "").Replace("\" />", "");
            System.Reflection.Assembly ThisAssembly = typeof(ThisProject.Controllers.HomeController).Assembly;
            tp = ThisAssembly.GetType(@typeAsText);
    
            if (tp == null)//try another referenced project
            {
                System.Reflection.Assembly externalAssembly = typeof(MyExternalReferncedProject.AnyClassInIt).Assembly;
                tp = externalAssembly.GetType(@typeAsText);
            }  
    
            if (tp == null)//also another referenced project- as needed
            {
                System.Reflection.Assembly anotherExtAssembly = typeof(MyExternalReferncedProject2.AnyClassInIt).Assembly;
                tp = anotherExtAssembly .GetType(@typeAsText);
            }
    
    
            if(tp == null)//case of nested class
            {
                System.Reflection.Assembly thisAssembly = typeof(ThisProject.Controllers.HomeController).Assembly;
                //the below code is done to support detecting nested classes.
                var processedTypeString = typeAsText;
                var lastIndexofPoint = typeAsText.LastIndexOf('.');
                while (lastIndexofPoint > 0 && tp == null)
                {
                    processedTypeString = processedTypeString.Insert(lastIndexofPoint, "+").Remove(lastIndexofPoint + 1, 1);
                    tp = SPLocatorBLLAssembly.GetType(processedTypeString);//nested class are recognized as: namespace.outerClass+nestedClass
                    lastIndexofPoint = processedTypeString.LastIndexOf('.');
                }
            }
    
            if (@tp != null)
            {
                ModelDescription md = modelDescriptionGenerator.GetOrCreateModelDescription(tp);
                    @Html.DisplayFor(m => md.ModelType, "ModelDescriptionLink", new { modelDescription = md })            
            }
            else
            {            
                    @MvcHtmlString.Create(@typeAsText)            
            }
            index = xmlSee.Index + xmlSee.Length;
            xmlSee = xmlSee.NextMatch();
        }    
                @MvcHtmlString.Create(@xml.Substring(@index, @xml.Length - @index))    
    }
    else
    {    
            @MvcHtmlString.Create(@xml);    
    }
    

    1. Finally Go to: ProjectName\Areas\HelpPage\Views\Help\DisplayTemplates**Parameters.cshtml**
      at line '20' we have the code corresponding to the Description in the documentation.
      REPLACE this:

                  <td class="parameter-documentation">
                      <p>
                          @parameter.Documentation
                      </p>
                  </td>
      

    With THIS:

                    <td class="parameter-documentation">
                        <p>
                            @Html.Partial("_XML_SeeTagsRenderer", (@parameter.Documentation == null? "" : @parameter.Documentation.ToString()))
                        </p>
                    </td>
    

    & Voila you must have it working now.
    Notes:

    • i tried putting HTML list inside the docs and it rendered it fine
    • i tried multiple class references (multiple <see cref="MyClass"> and i worked fine
    • you can't refer to a class that is declared within a class
    • when you refer to a class that is outside the current project please add the assembly .getType of a class within that project (check my code above)
    • any un-found class found inside a <see cref> will have it's full name printed in the description (for example if you reference a property or namespace, the code won't identify it as a type/class but it will be printed)

    这篇关于ASP的Web API帮助页面 - 链接到从XML&LT类;见&GT;标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆