C# 提供带有动态查询字符串的ASP.NET Web Api方法的文档

C# 提供带有动态查询字符串的ASP.NET Web Api方法的文档,c#,asp.net,asp.net-web-api,C#,Asp.net,Asp.net Web Api,我正在使用为我们的web api创建文档。使用XML文档注释,一切正常。然而,对于一种方法,我不知道如何为动态查询字符串提供文档。 该方法使用请求的GetQueryNameValuePairs()来选择模型的查询字符串的键值对。例如,?1=foo&2=bar将生成一个包含两个对象的列表,Id分别设置为1和2,值分别设置为“foo”和“bar” 我尝试将标记添加到XML注释中,但由于该方法不包含匹配参数,因此忽略了该标记 任何帮助都将不胜感激。您可以尝试扩展帮助页面生成过程。创建ASP.NET W

我正在使用为我们的web api创建文档。使用XML文档注释,一切正常。然而,对于一种方法,我不知道如何为动态查询字符串提供文档。 该方法使用请求的
GetQueryNameValuePairs()
来选择模型的查询字符串的键值对。例如,
?1=foo&2=bar
将生成一个包含两个对象的列表,Id分别设置为1和2,值分别设置为“foo”和“bar”

我尝试将
标记添加到XML注释中,但由于该方法不包含匹配参数,因此忽略了该标记


任何帮助都将不胜感激。

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

下面是我要做的:

  • 创建一个
    属性
    类并用它装饰我的特殊方法(例如
    [DynamicQueryParameter(“Param1”,typeof(string))]
  • 修改
    HelPageConfigurationExtensions.cs
    以从操作中查询这些属性,并手动将它们添加到模型的
    UriParameters
    集合中。我可能会在
    GenerateUriParameters()
    方法中这样做
  • [编辑]实际上我有一些时间,所以我自己制定了解决方案,因为,你知道,这很有趣:)

    因此,创建一个
    属性

    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" };
    }
    
    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;
    }