2017-03-09 51 views
1

我使用Help Pages for ASP.NET Web API为我们的web api创建文档。使用XML文档注释一切正常。但是,对于一种方法,我无法弄清楚如何为动态查询字符串提供文档。 该方法使用请求的GetQueryNameValuePairs()来将查询字符串的键值对选择到模型中。例如,?1=foo&2=bar将生成两个对象的列表,其中Id分别设置为1和2,值分别为'foo'和'bar'。使用动态查询字符串提供ASP.NET Web Api方法的文档

我已经尝试将<param>标记添加到XML注释中,但由于该方法不包含匹配的参数,因此会被忽略。

任何帮助,将不胜感激。

回答

0

您可以尝试扩展帮助页面生成过程。当您创建ASP.NET Web API项目时,与帮助页面相关的代码将作为源代码下载,而不是作为.dll下载,因此您可以使用任何自定义逻辑来扩展它。

这里是我会做:

  1. 创建attribute类和装饰我的特殊方法与(如[DynamicQueryParameter("Param1",typeof(string))]
  2. 修改HelPageConfigurationExtensions.cs查询到的操作以及这些属性和手动添加到该模型的UriParameters集合。我可能会在GenerateUriParameters()方法中执行此操作。

[编辑]其实我有一段时间了,所以我放在一起的解决方案我自己,因为,你知道,这很有趣:)

所以创建一个attribute

public class DynamicUriParameterAttribute : Attribute 
{ 
    public string Name { get; set; } 
    public Type Type { get; set; } 
    public string Description { get; set; } 
} 

你可以装饰这个动作方法:

[DynamicUriParameter(Description = "Some description", Name ="Some name", Type =typeof(string))] 
public IEnumerable<string> Get() 
{ 
    return new string[] { "value1", "value2" }; 
} 

然后我修改了HelpPageConfigurationExtensions.GenerateApiModel()像这样:

private static HelpPageApiModel GenerateApiModel(ApiDescription apiDescription, HttpConfiguration config) 
{ 
    HelpPageApiModel apiModel = new HelpPageApiModel() 
    { 
     ApiDescription = apiDescription, 
    }; 


ModelDescriptionGenerator modelGenerator = config.GetModelDescriptionGenerator(); 
HelpPageSampleGenerator sampleGenerator = config.GetHelpPageSampleGenerator();    
GenerateUriParameters(apiModel, modelGenerator); 

// add this part 
var attrs = apiDescription.ActionDescriptor.GetCustomAttributes<DynamicUriParameterAttribute>(); 
foreach (var attr in attrs) 
{ 
    apiModel.UriParameters.Add(
     new ParameterDescription 
     { 
      Name = attr.Name, 
      Documentation = attr.Description, 
      TypeDescription = modelGenerator.GetOrCreateModelDescription(attr.Type) 
     } 
    ); 
    } 
    // until here 
    GenerateRequestModelDescription(apiModel, modelGenerator, sampleGenerator); 
    GenerateResourceDescription(apiModel, modelGenerator); 
    GenerateSamples(apiModel, sampleGenerator); 

    return apiModel; 
}