日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

1.0 添加WEB API项目并按注释生成文档(多项目结构)

發布時間:2023/12/20 编程问答 39 豆豆
生活随笔 收集整理的這篇文章主要介紹了 1.0 添加WEB API项目并按注释生成文档(多项目结构) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

1.新建ASP.NET 項目,模板選擇如圖

2.選擇Web API,并選擇不進行身份驗證方式

成功后我們看到這個結果。

至于其它三種身份驗證方式,不太適合我的使用。而且這種方式也可以在代碼里去實現身份驗證,比較符合已有數據庫的情況,這里不寫。

3.給項目進行配置修改

3.1 修改對應項目生成路徑。

?

這是單項目結構的,如果有多個項目并且有引用,把其它項目生成XML的地址改為接口項目的地址,當然也可以生成后再拷貝過去。

3.2修改WebAPITest\Areas\HelpPage\App_Start\HelpPageConfig.cs里面的代碼。

反注釋

config.SetDocumentationProvider(new XmlDocumentationProvider(HttpContext.Current.Server.MapPath("~/App_Data/XmlDocument.xml")));

  里面的文檔改成我們上面生成的文檔名稱,打開help接口就能看到注釋了。

如果是引用了多個項目,就麻煩點。

先把剛剛這句改一下

config.SetDocumentationProvider(new XmlDocumentationProvider(HttpContext.Current.Server.MapPath("~/App_Data")));

  看見XmlDocumentationProvider這個類沒有?轉到定義過去看看,下面是原來的代碼,不用看了。

?

/// <summary>/// A custom <see cref="IDocumentationProvider"/> that reads the API documentation from an XML documentation file./// </summary>public class XmlDocumentationProvider : IDocumentationProvider, IModelDocumentationProvider{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 PropertyExpression = "/doc/members/member[@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="documentPath">The physical path to XML document.</param>public XmlDocumentationProvider(string documentPath){if (documentPath == null){throw new ArgumentNullException("documentPath");}XPathDocument xpath = new XPathDocument(documentPath);_documentNavigator = xpath.CreateNavigator();}public string GetDocumentation(HttpControllerDescriptor controllerDescriptor){XPathNavigator typeNode = GetTypeNode(controllerDescriptor.ControllerType);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 = 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");}public string GetDocumentation(MemberInfo member){string memberName = String.Format(CultureInfo.InvariantCulture, "{0}.{1}", GetTypeName(member.DeclaringType), member.Name);string expression = member.MemberType == MemberTypes.Field ? FieldExpression : PropertyExpression;string selectExpression = String.Format(CultureInfo.InvariantCulture, expression, memberName);XPathNavigator propertyNode = _documentNavigator.SelectSingleNode(selectExpression);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 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}", 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 _documentNavigator.SelectSingleNode(selectExpression);}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 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.name = name.Replace("+", ".");}return name;}} View Code

?

  我們把它改下,整個類替換掉,沒引用的自己右鍵引用。

?

public class XmlDocumentationProvider : IDocumentationProvider, IModelDocumentationProvider{// private XPathNavigator _documentNavigator;private List<XPathNavigator> _documentNavigators = new List<XPathNavigator>();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/member[@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="documentPath">The physical path to XML document.</param>//public XmlDocumentationProvider(string documentPath)//{// if (documentPath == null)// {// throw new ArgumentNullException("documentPath");// }// XPathDocument xpath = new XPathDocument(documentPath);// _documentNavigator = xpath.CreateNavigator();//}/// <summary>/// Initializes a new instance of the <see cref="XmlDocumentationProvider"/> class./// </summary>/// <param name="appDataPath">The physical path to XML document.</param>public XmlDocumentationProvider(string appDataPath){if (appDataPath == null){throw new ArgumentNullException("appDataPath");}var files = new[] { "AuthSystem.xml", "AuthSystem.SQL.xml", "AuthSystem.Common.XML" };//這里是你其它dll的說明路徑,自己可以改寫。例如 var files = Directory.GetFiles(appDataPath, "TLSC.*.xml");//獲取所有類似的xmlforeach (var file in files){XPathDocument xpath = new XPathDocument(Path.Combine(appDataPath, file));_documentNavigators.Add(xpath.CreateNavigator());}}private XPathNavigator SelectSingleNode(string selectExpression){foreach (var navigator in _documentNavigators){var propertyNode = navigator.SelectSingleNode(selectExpression);if (propertyNode != null)return propertyNode;}return null;}public string GetDocumentation(HttpControllerDescriptor controllerDescriptor){XPathNavigator typeNode = GetTypeNode(controllerDescriptor.ControllerType);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 = 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");}public string GetDocumentation(MemberInfo member){string memberName = String.Format(CultureInfo.InvariantCulture, "{0}.{1}", GetTypeName(member.DeclaringType), member.Name);string expression = member.MemberType == MemberTypes.Field ? FieldExpression : PropertyExpression;string selectExpression = String.Format(CultureInfo.InvariantCulture, expression, memberName);XPathNavigator propertyNode = SelectSingleNode(selectExpression);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 ReflectedHttpActionDescriptor;if (reflectedActionDescriptor != null){string selectExpression = String.Format(CultureInfo.InvariantCulture, MethodExpression, GetMemberName(reflectedActionDescriptor.MethodInfo));return SelectSingleNode(selectExpression);}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 SelectSingleNode(selectExpression);}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 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.name = name.Replace("+", ".");}return name;}} View Code

?

?

?

原理就是從只讀一個文檔改成讀取多個文檔。這樣就能在help中看到我們寫的注釋(summary)了。

?效果圖就不貼了,免得浪費大家流量哈哈。

轉載于:https://www.cnblogs.com/cvol/p/6836226.html

總結

以上是生活随笔為你收集整理的1.0 添加WEB API项目并按注释生成文档(多项目结构)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。