您可以尝试扩展帮助页面生成过程。当您创建ASP.NET Web API项目时,与帮助页面相关的代码将作为源代码下载,而不是作为.dll
下载,因此您可以使用任何自定义逻辑来扩展它。
这里是我会做:
- 创建
attribute
类和装饰我的特殊方法与(如[DynamicQueryParameter("Param1",typeof(string))]
)
- 修改
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;
}