Weiapi2.2 HelpPage automatically generates interface instructions and interface test functions,
When developing a Webapi project, do you need to add corresponding function descriptions and test cases when writing a method? It provides a convenient way to describe the API documentation and test HelpPage more easily.
Her general principle is: The. dll assembly and. xml assembly description files will be generated during compilation, and the Controller name, action name, parameter information, and remarks information will be obtained through the xml file. In this way, the interface instructions can be put into the remarks, and I personally think it is really rude and simple. Where is the interface test? The third-party nuget package webapitestclient is used here.
First!
The case was created with VS2013 and HelpPage has been created, but wepapi version is 1.0. The wepapi2 feature is enhanced to enable nuget upgrade at a higher pace.
Other interdependent items will also be upgraded!
Set the path of the xml Description document:
The xml Path generated by setting web project properties:
Sorry, webapitestclient only supports the lowest version of HelpPage. You have to modify some code to upgrade webapi! Note: webapi1 can get the action remarks but cannot get the controller remarks, which means webapi2 can.
After the upgrade, the XmlDocumentationProvider class requires two more implementation methods: Controller and action Description.
XmlDocumentationProvider.cs
public class XmlDocumentationProvider : IDocumentationProvider { private XPathNavigator _documentNavigator; private const string TypeExpression = "/doc/members/member[@name='T:{0}']"; private const string MethodExpression = "/doc/members/member[@name='M:{0}']"; private const string ParameterExpression = "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> public XmlDocumentationProvider(string documentPath="") { //if (documentPath.IsNullOrWhiteSpace()) // documentPath = HttpContext.Current.Server.MapPath(ConfigurationManager.AppSettings["webApiDescription"]); if (documentPath == null) { throw new ArgumentNullException("documentPath"); } XPathDocument xpath = new XPathDocument(documentPath); _documentNavigator = xpath.CreateNavigator(); } private XPathNavigator GetTypeNode(Type type) { string controllerTypeName = GetTypeName(type); string selectExpression = String.Format(CultureInfo.InvariantCulture, TypeExpression, controllerTypeName); return _documentNavigator.SelectSingleNode(selectExpression); } 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; } public virtual string GetDocumentation(HttpControllerDescriptor controllerDescriptor) { XPathNavigator typeNode = GetTypeNode(controllerDescriptor.ControllerType); return GetTagValue(typeNode, "summary"); } public virtual string GetDocumentation(HttpActionDescriptor actionDescriptor) { XPathNavigator methodNode = GetMethodNode(actionDescriptor); if (methodNode != null) { XPathNavigator summaryNode = methodNode.SelectSingleNode("summary"); if (summaryNode != null) { return summaryNode.Value.Trim(); } } return null; } public virtual string GetDocumentation(HttpParameterDescriptor parameterDescriptor) { ReflectedHttpParameterDescriptor reflectedParameterDescriptor = parameterDescriptor as ReflectedHttpParameterDescriptor; if (reflectedParameterDescriptor != null) { XPathNavigator methodNode = GetMethodNode(reflectedParameterDescriptor.ActionDescriptor); if (methodNode != null) { string parameterName = reflectedParameterDescriptor.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"); } private XPathNavigator GetMethodNode(HttpActionDescriptor actionDescriptor) { ReflectedHttpActionDescriptor reflectedActionDescriptor = actionDescriptor as ReflectedHttpActionDescriptor; if (reflectedActionDescriptor != null) { string selectExpression = String.Format(CultureInfo.InvariantCulture, MethodExpression, GetMemberName(reflectedActionDescriptor.MethodInfo)); return _documentNavigator.SelectSingleNode(selectExpression); } return null; } private static string GetMemberName(MethodInfo method) { string name = String.Format(CultureInfo.InvariantCulture, "{0}.{1}", method.DeclaringType.FullName, 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 GetTypeName(Type type) { 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 typeName = genericType.FullName; // Trim the generic parameter counts from the name typeName = typeName.Substring(0, typeName.IndexOf('`')); string[] argumentTypeNames = genericArguments.Select(t => GetTypeName(t)).ToArray(); return String.Format(CultureInfo.InvariantCulture, "{0}{{{1}}}", typeName, String.Join(",", argumentTypeNames)); } return type.FullName; } }
Modify and obtain the Controller information:
HelpController. cs
Index. cshtml
ApiGroup. cshtml
public ActionResult Index() { ViewBag.DocumentationProvider = Configuration.Services.GetDocumentationProvider(); return View(Configuration.Services.GetApiExplorer().ApiDescriptions); }
@ Model Collection <apide.pdf >@{ ViewBag. title = "ASP. NET Web API Help Page "; // Group APIs by controller ILookup <System. web. http. controllers. httpControllerDescriptor, apide.pdf> apiGroups = Model. toLookup (api => api. actionDescriptor. controllerDescriptor);}
@ Model IGrouping <System. Web. Http. Controllers. HttpControllerDescriptor, apide.pdf >@{ var controllerDocumentation = ViewBag. DocumentationProvider! = Null? ViewBag. DocumentationProvider. GetDocumentation (Model. Key): null ;}<! -- Controller name -->
The effect is as follows:
Next, add the Interface Test function.
Add webapitestclient to nuget:
Go to the "retrieve single item information" interface page. The "Test Api" button is displayed, and you can modify the location by yourself!
Click "Test Api" to bring up the call window:
Input parameter call and output json data:
Shared Demo
Webapi2.2WithTest.zip
By HsutonWang
Source: http://www.cnblogs.com/AntonWang/p/4192119.html/
This article is copyrighted by the author and the blog site.