Web API entity display comments and webapi entity comments

Source: Internet
Author: User

Web API entity display comments and webapi entity comments

I think the comments about Web APIs in the garden are about methods, and there is no way to display entity comments. Today I spent some time doing this.

In fact, the display of comments is generated based on the XML document file of the class library.

First, you need to generate an XML document file using the Class Library:

Modify the following in the HelpPageConfig class to add multiple XML files for the class library. You can only add one

MultipleXmlDocumentationProvider code, which is basically similar to XmlDocumentationProvider

/// <Summary> /// multiple XML annotations /// </summary> public class MultipleXmlDocumentationProvider: IDocumentationProvider, IModelDocumentationProvider {private IList <XPathNavigator> _ documentNavigators; private const string TypeExpression = "/doc/members/member [@ name ='t: {0} ']"; private const string MethodExpression = "/doc/members/member [@ name ='m: {0} ']"; private const string PropertyExpression = "/doc/members/M Ember [@ name = 'P: {0} '] "; private const string FieldExpression ="/doc/members/member [@ name = 'f: {0} '] "; private const string ParameterExpression =" param [@ name =' {0} '] "; /// <summary> // Initializes a new instance of the <see cref = "XmlDocumentationProvider"/> class. /// </summary> /// <param name = "documentPaths"> List of physical paths to XML documents ENTs. </param> public MultipleXmlDocumentationProvider (p Arams string [] documentPaths) {if (! DocumentPaths. any () {throw new ArgumentNullException ("documentPaths");} // read all XML documents var documents = documentPaths. where (p => File. exists (p )). toList (); _ documentNavigators = documents. select (p => new XPathDocument (p ). createNavigator ()). toList ();} public string GetDocumentation (HttpControllerDescriptor controllerDescriptor) {XPathNavigator typeNode = GetTypeNode (controllerDescriptor. controllerTy Pe); return GetTagValue (typeNode, "summary");} public virtual string GetDocumentation (HttpActionDescriptor actionDescriptor) {XPathNavigator methodNode = GetMethodNode (actionDescriptor); return GetTagValue (methodNode, "summary");} public virtual string GetDocumentation (HttpParameterDescriptor parameterDescriptor) {ReflectedHttpParameterDescriptor reflectedParameterDescriptor = parameterDescrip Tor as ReflectedHttpParameterDescriptor; if (reflectedParameterDescriptor! = Null) {XPathNavigator methodNode = GetMethodNode (reflectedParameterDescriptor. ActionDescriptor); if (methodNode! = Null) {string parameterName = parameters. ParameterInfo. Name; XPathNavigator parameterNode = methodNode. SelectSingleNode (String. Format (CultureInfo. InvariantCulture, ParameterExpression, parameterName); if (parameterNode! = Null) {return parameterNode. value. trim () ;}}return null;} public string GetResponseDocumentation (HttpActionDescriptor actionDescriptor) {XPathNavigator methodNode = GetMethodNode (actionDescriptor); return GetTagValue (methodNode, "returns ");} public string GetDocumentation (MemberInfo member) {string memberName = String. format (CultureInfo. invariantCulture, "{0 }. {1} ", GetTypeName (member. dec LaringType), member. Name); string expression = member. MemberType = MemberTypes. Field? FieldExpression: PropertyExpression; string selectExpression = String. format (CultureInfo. invariantCulture, expression, memberName); var propertyNode = _ documentNavigators. select (n => n. selectSingleNode (selectExpression )). firstOrDefault (n => n! = Null); return GetTagValue (propertyNode, "summary");} public string GetDocumentation (Type type) {XPathNavigator typeNode = GetTypeNode (type); return GetTagValue (typeNode, "summary");} private XPathNavigator GetMethodNode (HttpActionDescriptor actionDescriptor) {ReflectedHttpActionDescriptor reflectedActionDescriptor = actionDescriptor as Callback; if (reflectedActionDescrip Tor! = Null) {string selectExpression = String. format (CultureInfo. invariantCulture, MethodExpression, GetMemberName (reflectedActionDescriptor. methodInfo); return _ documentNavigators. select (n => n. selectSingleNode (selectExpression )). firstOrDefault (n => n! = Null);} return null;} private static string GetMemberName (MethodInfo method) {string name = 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);} return name;} private static string GetTagValue (XPathNavigator parentNode, string tagName) {if (parentNode! = Null) {XPathNavigator node = parentNode. SelectSingleNode (tagName); if (node! = Null) {return node. value. trim () ;}return null;} private XPathNavigator GetTypeNode (Type type) {string controllerTypeName = GetTypeName (type); string selectExpression = String. format (CultureInfo. invariantCulture, TypeExpression, controllerTypeName); return _ documentNavigators. select (n => n. selectSingleNode (selectExpression )). firstOrDefault (n => n! = Null);} private static string GetTypeName (Type type) {string name = 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 (); string genericTypeName = genericType. fullName; // Trim the generic parameter counts from the name genericTypeName = 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. name = name. replace ("+ ",". ") ;}return name ;}}

 

Shown successfully:

Reference link:

Http://stackoverflow.com/questions/21393754/helppage-xml-documentation-when-controllers-or-models-in-different-assembly

 

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.