WEBAPI upgrade to the Xmldocumentationprovider after 2.0

Source: Internet
Author: User

usingSystem;usingSystem.Globalization;usingSystem.Linq;usingSystem.Reflection;usingSystem.Web.Http.Controllers;usingSystem.Web.Http.Description;usingSystem.Xml.XPath;usingTiza.CStar.ErpApi.Areas.HelpPage.ModelDescriptions;namespacetiza.cstar.erpapi.areas.helppage{/// <summary>    ///A Custom<see cref= "Idocumentationprovider"/>That reads the API documentation from the XML documentation file. /// </summary>     Public classXmldocumentationprovider:idocumentationprovider, Imodeldocumentationprovider {PrivateXPathNavigator _documentnavigator; Private Const stringTypeexpression ="/doc/members/member[@name = ' t:{0} ']"; Private Const stringMethodexpression ="/doc/members/member[@name = ' m:{0} ']"; Private Const stringPropertyexpression ="/doc/members/member[@name = ' p:{0} ']"; Private Const stringFieldexpression ="/doc/members/member[@name = ' f:{0} ']"; Private Const stringParameterExpression ="param[@name = ' {0} ']"; /// <summary>        ///Initializes a new instance of the<see cref= "Xmldocumentationprovider"/>class. /// </summary>        /// <param name= "Documentpath" >The physical path to XML document.</param>         PublicXmldocumentationprovider (stringdocumentpath) {            if(Documentpath = =NULL)            {                Throw NewArgumentNullException ("documentpath"); } XPathDocument XPath=NewXPathDocument (documentpath); _documentnavigator=XPath.        CreateNavigator (); }         Public stringgetdocumentation (Httpcontrollerdescriptor controllerdescriptor) {XPathNavigator typenode=Gettypenode (Controllerdescriptor.controllertype); returnGettagvalue (Typenode,"Summary"); }         Public Virtual stringgetdocumentation (Httpactiondescriptor actiondescriptor) {XPathNavigator methodnode=Getmethodnode (Actiondescriptor); returnGettagvalue (Methodnode,"Summary"); }         Public Virtual stringgetdocumentation (Httpparameterdescriptor parameterdescriptor) {Reflectedhttpparameterdescriptor re Flectedparameterdescriptor= Parameterdescriptor asReflectedhttpparameterdescriptor; if(Reflectedparameterdescriptor! =NULL) {XPathNavigator methodnode=Getmethodnode (Reflectedparameterdescriptor.actiondescriptor); if(Methodnode! =NULL)                {                    stringParameterName =ReflectedParameterDescriptor.ParameterInfo.Name; XPathNavigator Parameternode=Methodnode.selectsinglenode (String.Format (CultureInfo.InvariantCulture, ParameterExpression, ParameterName))                    ; if(Parameternode! =NULL)                    {                        returnParameterNode.Value.Trim (); }                }            }            return NULL; }         Public stringgetresponsedocumentation (Httpactiondescriptor actiondescriptor) {XPathNavigator methodnode=Getmethodnode (Actiondescriptor); returnGettagvalue (Methodnode,"returns"); }         Public stringGetdocumentation (MemberInfo member) {stringMemberName = String.Format (CultureInfo.InvariantCulture,"{0}. {1}", Gettypename (member. DeclaringType), member.            Name); stringexpression = member. MemberType = = Membertypes.field?fieldexpression:propertyexpression; stringSelectexpression =String.Format (cultureinfo.invariantculture, expression, membername); XPathNavigator Propertynode=_documentnavigator.selectsinglenode (selectexpression); returnGettagvalue (Propertynode,"Summary"); }         Public stringgetdocumentation (Type type) {XPathNavigator Typenode=Gettypenode (type); returnGettagvalue (Typenode,"Summary"); }        PrivateXPathNavigator Getmethodnode (Httpactiondescriptor actiondescriptor) {Reflectedhttpactiondescriptor Reflectedactiondescriptor= Actiondescriptor asReflectedhttpactiondescriptor; if(Reflectedactiondescriptor! =NULL)            {                stringSelectexpression =String.Format (CultureInfo.InvariantCulture, Methodexpression, Getmembername (                Reflectedactiondescriptor.methodinfo)); return_documentnavigator.selectsinglenode (selectexpression); }            return NULL; }        Private Static stringGetmembername (MethodInfo method) {stringName = String.Format (CultureInfo.InvariantCulture,"{0}. {1}", Gettypename (method. DeclaringType), method.            Name); parameterinfo[] Parameters=method.            GetParameters (); if(Parameters. Length! =0)            {                string[] parametertypenames = parameters. Select (param =gettypename (param. ParameterType)).                ToArray (); Name+ = String.Format (CultureInfo.InvariantCulture,"({0})", String.Join (",", Parametertypenames)); }            returnname; }        Private Static stringGettagvalue (XPathNavigator parentnode,stringtagName) {            if(ParentNode! =NULL) {XPathNavigator node=Parentnode.selectsinglenode (tagName); if(Node! =NULL)                {                    returnnode.                Value.trim (); }            }            return NULL; }        PrivateXPathNavigator Gettypenode (type type) {stringControllertypename =gettypename (type); stringSelectexpression =String.Format (CultureInfo.InvariantCulture, typeexpression, controllertypename); return_documentnavigator.selectsinglenode (selectexpression); }        Private Static stringgettypename (Type type) {stringName =type.            FullName; if(type. Isgenerictype) {//Format The generic type name to something like:generic{system.int32,system.string}Type GenericType =type.                GetGenericTypeDefinition (); Type[] Genericarguments=type.                GetGenericArguments (); stringGenerictypename =Generictype.fullname; //Trim The generic parameter counts from the nameGenerictypename = generictypename.substring (0, Generictypename.indexof ('`')); string[] Argumenttypenames = genericarguments.select (t =Gettypename (t)).                ToArray (); Name= String.Format (CultureInfo.InvariantCulture,"{0}{{{1}}}", Generictypename, String.Join (",", Argumenttypenames)); }            if(type. isnested) {//changing the nested type name from Outertype+innertype to Outertype.innertype to match the XML documentation syntax.< /c1>Name = name. Replace ("+","."); }            returnname; }    }}

WEBAPI upgrade to the Xmldocumentationprovider after 2.0

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.