ASP.NETWebAPI从模型生成所有参数-帮助页面我正在使用asp.net网站上建议的库来生成文档(http://www.asp.net/web-api/overview/creating-web-apis/creating-api-help-pages)。我的问题是,如果我的参数是一个模型,那么我无法在生成的帮助页面中指定该模型包含哪些属性。这是一个示例:模型:公共类TestModel{propertyStringFirstName{get;set;}propertyStringSurname{get;set;}propertyBooleanActive{get;set;}}Action://////这是一个测试动作//////这是模型<--这是可行的///这是名字<--不起作用///Thisisthesurname<--不起作用publicHttpResponseMessagePost(my.namespace.models.TestModelmodel){...}仅生成模型的参数。我查看了为文档生成的xml文档,它确实添加了额外的参数。这是一个测试动作这是模型这是名字这是姓氏但是在帮助页面上它只生成参数模型。我已经追踪到从xml获取参数的方法。集合apiDescriptions=config.Services.GetApiExplorer().ApiDescriptions;它位于自动生成的HelpPageConfigurationExtentions.cs中。我是以错误的方式接近这个吗?有谁知道解决方法?任何建议或解决方案将不胜感激。MVCWebAPI文档功能使用反射遍历您的API类和方法。这将构建文档的结构,但除非您添加文档注释,否则将导致或多或少的空(和无用)文档。使用使用///文档注释生成的XML文件填充文档文本,这些注释具有必须遵循的特定结构。这意味着你不能用你想要显示的任何东西填充你的xml,它实际上必须连接到你的API中的某些东西并且必须遵循你的类和属性的结构。因此,在您的情况下,您不能将模型属性文档放入api方法中。您必须将它放在属性存在的模型中。Model:publicclassTestModel{///这是名字属性StringFirstName{get;set;}///这是姓氏属性StringSurname{get;set;}propertyBooleanActive{get;set;}}Action://////这是一个测试动作//////这是模型publicHttpResponseMessagePost(my.namespace.models.TestModelmodel){...}修改帮助页面自动生成的默认帮助页面不包含模型文档,仅记录API方法。为了在api中显示更多关于参数的信息,需要定制。下面的说明是添加参数文档的一种方法。在Areas/HelpPage/Models中创建两个新类型publicclassTypeDocumentation{publicTypeDocumentation(){PropertyDocumentation=newCollection();}公共字符串摘要{得到;放;}publicICollectionPropertyDocumentation{得到;放;}}publicclassPropertyDocumentation{publicPropertyDocumentation(stringname,stringtype,stringdocs){Name=name;类型=类型;文档=文档;}公共字符串名称{得到;放;}公共字符串类型{得到;放;}公共字符串文档{get;放;}}添加一个新属性到HelpPageApiModel.cspublicIDictionaryParameterModels{get;放;}创建一个新的接口internalinterfaceIModelDocumentationProvider{IDictionaryGetModelDocumentation(HttpActionDescriptoractionDescriptor);}修改XmlDocumentationProvider以实现新接口publicclassXmlDocumentationProvidationProviderIModelDocumentationProvider{privateconststringTypeExpression="/doc/members/member[@name='T:{0}']";私有常量字符串PropertyExpression="/doc/members/member[@name='P:{0}']";///...///...现有代码///...privatestaticstringGetPropertyName(PropertyInfoproperty){stringname=String.Format(CultureInfo.InvariantCulture,"{0}.{1}",property.DeclaringType.FullName,property.Name);返回名称;}publicIDictionaryGetModelDocumentation(HttpActionDescriptoractionDescriptor){varretDictionary=newDictionary();ReflectedHttpActionDescriptorreflectedActionDescriptor=actionDescriptorasReflectedHttpActionDescriptor;if(reflectedActionDescriptor!=null){foreach(varparameterDescriptorinreflectedActionDescriptor.GetParameters()){if(!parameterDescriptor.ParameterType.IsValueType){TypeDocumentationtypeDocs=newTypeDocumentation();字符串selectExpression=String.Format(CultureInfo.InvariantCulture,TypeExpression,GetTypeName(parameterDescriptor.ParameterType));vartypeNode=_documentNavigator.SelectSingleNode(selectExpression);如果(类型节点!=null){X路径导航器摘要节点;summaryNode=typeNode.SelectSingleNode("总结");if(summaryNode!=null)typeDocs.Summary=summaryNode.Value;}foreach(varpropinparameterDescriptor.ParameterType.GetProperties()){stringpropName=prop.Name;stringpropDocs=string.Empty;字符串propExpression=String.Format(CultureInfo.InvariantCulture,PropertyExpression,GetPropertyName(prop));varpropNode=_documentNavigator.SelectSingleNode(propExpression);if(propNode!=null){XPathNavigatorsummaryNode;summaryNode=propNode.SelectSingleNode("总结");if(summaryNode!=null)propDocs=summaryNode.Value;}typeDocs.PropertyDocumentation.Add(newPropertyDocumentation(propName,prop.PropertyType.Name,propDocs));}retDictionary.Add(parameterDescriptor.ParameterName,typeDocs);}}}返回retDictionary;在GenerateApiModel方法中将代码添加到HelpPageConfigurationExtensionIModelDocumentationProvidermodelProvider=config.Services.GetDocumentationProvider()作为IModelDocumentationProvider;如果(modelProvider!=null){apiModel.ParameterModels=modelProvider.GetModelDocumentation(apiDescription.ActionDescriptor);}修改HelpPageApiModel.cshtml添加到你希望模型文档显示的地方boolhasModels=Model.ParameterModels.Count>0;if(hasModels){ParameterInformation@Html.DisplayFor(apiModel=>apiModel.ParameterModels,"Models")}添加一个Models.cshtml到DisplayTemplates@usingSystem.Web.Http@usingSystem.Web.Http.Description@usingMvcApplication2.Areas.HelpPage.Models@modelIDictionary@foreach(varmodelTypeinModel){@modelType.Keyif(modelType.Value.Summary!=null){@modelType.Value.Summary}属性描述@foreach(varpropInfoinmodelType.Value.PropertyDocumentation){@propInfo.Name(@propInfo.Type)@propInfo.Documentation}}约瑟夫的回答有效。但我确实发现它有点过分热心。我发现它将简单的东西(例如字符串)报告为模型,并将它们报告为带有长度字段的Char数组!我们只需要这个模型,所以我将这段代码添加到GetModelDocumentation方法的末尾:);现在它只返回非简单类型的参数详细信息。以上就是C#学习教程:ASP.NETWebAPI生成model-help页面的所有内容共享所有参数。如果对大家有用,需要了解更多C#学习教程,希望大家多多关注---本文来自网络收藏,不代表立场,如涉及侵权,请右击联系管理员删除。如需转载请注明出处:
