Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. using System;
  2. using System.Globalization;
  3. using System.Linq;
  4. using System.Reflection;
  5. using System.Web.Http.Controllers;
  6. using System.Web.Http.Description;
  7. using System.Xml.XPath;
  8. namespace WebAPiUtils_test.Areas.HelpPage
  9. {
  10. /// <summary>
  11. /// A custom <see cref="IDocumentationProvider"/> that reads the API documentation from an XML documentation file.
  12. /// </summary>
  13. public class XmlDocumentationProvider : IDocumentationProvider
  14. {
  15. private XPathNavigator _documentNavigator;
  16. private const string TypeExpression = "/doc/members/member[@name='T:{0}']";
  17. private const string MethodExpression = "/doc/members/member[@name='M:{0}']";
  18. private const string ParameterExpression = "param[@name='{0}']";
  19. /// <summary>
  20. /// Initializes a new instance of the <see cref="XmlDocumentationProvider"/> class.
  21. /// </summary>
  22. /// <param name="documentPath">The physical path to XML document.</param>
  23. public XmlDocumentationProvider(string documentPath)
  24. {
  25. if (documentPath == null)
  26. {
  27. throw new ArgumentNullException("documentPath");
  28. }
  29. XPathDocument xpath = new XPathDocument(documentPath);
  30. _documentNavigator = xpath.CreateNavigator();
  31. }
  32. public string GetDocumentation(HttpControllerDescriptor controllerDescriptor)
  33. {
  34. XPathNavigator typeNode = GetTypeNode(controllerDescriptor);
  35. return GetTagValue(typeNode, "summary");
  36. }
  37. public virtual string GetDocumentation(HttpActionDescriptor actionDescriptor)
  38. {
  39. XPathNavigator methodNode = GetMethodNode(actionDescriptor);
  40. return GetTagValue(methodNode, "summary");
  41. }
  42. public virtual string GetDocumentation(HttpParameterDescriptor parameterDescriptor)
  43. {
  44. ReflectedHttpParameterDescriptor reflectedParameterDescriptor = parameterDescriptor as ReflectedHttpParameterDescriptor;
  45. if (reflectedParameterDescriptor != null)
  46. {
  47. XPathNavigator methodNode = GetMethodNode(reflectedParameterDescriptor.ActionDescriptor);
  48. if (methodNode != null)
  49. {
  50. string parameterName = reflectedParameterDescriptor.ParameterInfo.Name;
  51. XPathNavigator parameterNode = methodNode.SelectSingleNode(String.Format(CultureInfo.InvariantCulture, ParameterExpression, parameterName));
  52. if (parameterNode != null)
  53. {
  54. return parameterNode.Value.Trim();
  55. }
  56. }
  57. }
  58. return null;
  59. }
  60. public string GetResponseDocumentation(HttpActionDescriptor actionDescriptor)
  61. {
  62. XPathNavigator methodNode = GetMethodNode(actionDescriptor);
  63. return GetTagValue(methodNode, "returns");
  64. }
  65. private XPathNavigator GetMethodNode(HttpActionDescriptor actionDescriptor)
  66. {
  67. ReflectedHttpActionDescriptor reflectedActionDescriptor = actionDescriptor as ReflectedHttpActionDescriptor;
  68. if (reflectedActionDescriptor != null)
  69. {
  70. string selectExpression = String.Format(CultureInfo.InvariantCulture, MethodExpression, GetMemberName(reflectedActionDescriptor.MethodInfo));
  71. return _documentNavigator.SelectSingleNode(selectExpression);
  72. }
  73. return null;
  74. }
  75. private static string GetMemberName(MethodInfo method)
  76. {
  77. string name = String.Format(CultureInfo.InvariantCulture, "{0}.{1}", method.DeclaringType.FullName, method.Name);
  78. ParameterInfo[] parameters = method.GetParameters();
  79. if (parameters.Length != 0)
  80. {
  81. string[] parameterTypeNames = parameters.Select(param => GetTypeName(param.ParameterType)).ToArray();
  82. name += String.Format(CultureInfo.InvariantCulture, "({0})", String.Join(",", parameterTypeNames));
  83. }
  84. return name;
  85. }
  86. private static string GetTagValue(XPathNavigator parentNode, string tagName)
  87. {
  88. if (parentNode != null)
  89. {
  90. XPathNavigator node = parentNode.SelectSingleNode(tagName);
  91. if (node != null)
  92. {
  93. return node.Value.Trim();
  94. }
  95. }
  96. return null;
  97. }
  98. private static string GetTypeName(Type type)
  99. {
  100. if (type.IsGenericType)
  101. {
  102. // Format the generic type name to something like: Generic{System.Int32,System.String}
  103. Type genericType = type.GetGenericTypeDefinition();
  104. Type[] genericArguments = type.GetGenericArguments();
  105. string typeName = genericType.FullName;
  106. // Trim the generic parameter counts from the name
  107. typeName = typeName.Substring(0, typeName.IndexOf('`'));
  108. string[] argumentTypeNames = genericArguments.Select(t => GetTypeName(t)).ToArray();
  109. return String.Format(CultureInfo.InvariantCulture, "{0}{{{1}}}", typeName, String.Join(",", argumentTypeNames));
  110. }
  111. return type.FullName;
  112. }
  113. private XPathNavigator GetTypeNode(HttpControllerDescriptor controllerDescriptor)
  114. {
  115. Type controllerType = controllerDescriptor.ControllerType;
  116. string controllerTypeName = controllerType.FullName;
  117. if (controllerType.IsNested)
  118. {
  119. // Changing the nested type name from OuterType+InnerType to OuterType.InnerType to match the XML documentation syntax.
  120. controllerTypeName = controllerTypeName.Replace("+", ".");
  121. }
  122. string selectExpression = String.Format(CultureInfo.InvariantCulture, TypeExpression, controllerTypeName);
  123. return _documentNavigator.SelectSingleNode(selectExpression);
  124. }
  125. }
  126. }