我正在实现一个Web项目,它将使用标准的HelpPages区域作为文档。我在我的项目中使用属性路由并实现ApiVersioning。我已经记录了我的大多数方法和模型,但是我试图弄清楚如何记录API版本路由参数。下面是我的控制器的一个例子:
/// <summary>
/// Controller for the License api.
/// </summary>
[ApiVersion("1.0")]
[RoutePrefix("api/v{version:apiVersion}/license")]
public class LicenseController : ApiController
{
#region Software License Methods
/// <summary>
/// Creates a new Software License.
/// </summary>
/// <param name="value">The parameters for the license.</param>
/// <returns>The newly created Activation and Emergency Ids.</returns>
[Route("software")]
[HttpPost]
public LicenseCreateResponse CreateSoftwareLicense([FromBody] CreateSoftwareLicenseRequest value)
{
// License creation code
}
配置HelpArea并运行项目后,我获得以下帮助信息:
版本参数有一个可用的描述,但是我不知道如何记录它。就方法而言,这不是路由的一部分,因此尝试<param name="version">...
是徒劳的。
谢谢你的帮助!
发布于 2018-03-26 00:55:36
这个解决方案可能有效,但不需要那么困难。出于好奇,您是否正在使用官方的API资源管理器包进行API版本控制?看上去不像。IApiExplorer实现它为您提供了API版本参数的文档,包括描述。
如果您想要更改默认的说明文本,可以在API Explorer选项中这样做:
configuration.AddVersionedApiExplorer(
options => options.DefaultApiVersionParameterDescription = "The version" )
帮助页面似乎已经过时,倾向于Swagger/Swashbuckle,但是API版本化API资源管理器应该与它无缝地工作。如果有空白或其他疼痛点,我肯定会有兴趣听到他们。
发布于 2018-03-18 15:47:35
操作帮助页上的URI参数部分用于描述从URI查询字符串绑定的操作参数(例如,...?SomeParam=SomeValue
)。在您的示例中,version
只是URI路径的一部分,与操作参数无关。因此,在URI参数部分中描述它可能会让人感到困惑。这就是为什么我建议从本节中删除它,并(如果需要的话)将version
模板的描述放到帮助页面的其他部分。
要做到这一点,你应该:
version
。这一步并不简单,因为来自路由模板的version
被ApiExplorer
识别为动作参数。要抑制它,就需要修改生成的代码,以填充API帮助页面模型(HelpPageApiModel
)。此代码驻留在HelpPageConfigurationExtensions.GenerateUriParameters()
方法中。可以找到以下几行:
Debug.Assert(parameterDescriptor == null);//如果parameterDescriptor为null,则这是一个未声明的路由参数,仅在源为FromUri时发生//。在请求模型中和资源参数之间忽略,但在这里将//作为一个简单字符串列出。modelGenerator.GetOrCreateModelDescription(typeof(string));AddParameterDescription(apiModel,apiParameter,modelDescription);
并为version
参数添加条件:
Debug.Assert(parameterDescriptor == null);if (apiParameter.Documentation == null & String.Equals(apiParameter.Name,"version",StringComparison.InvariantCulture)) {==;}/如果parameterDescriptor为null,则这是一个未声明的路由参数,仅在源为FromUri时发生/。在请求模型中和资源参数之间忽略,但在这里将//作为一个简单字符串列出。modelGenerator.GetOrCreateModelDescription(typeof(string));AddParameterDescription(apiModel,apiParameter,modelDescription);
现在,您将获得以下帮助页面:
Areas\HelpPage\Views\Help\DisplayTemplates\HelpPageApiModel.cshtml
并添加所需的版本部分。例如:
..。请求信息版本信息在这里放置一些关于版本的信息。URI参数@Html.DisplayFor(m => m.UriParameters,参数).
这样的观点会让你:
但是,如果您想在URI参数部分看到版本描述,可以返回到HelpPageConfigurationExtensions.GenerateUriParameters()
方法并用以下代码替换上面的代码:
Debug.Assert(parameterDescriptor == null);
if (apiParameter.Documentation == null && String.Equals(apiParameter.Name, "version", StringComparison.InvariantCulture))
{
apiParameter.Documentation = "Put description for version parameter here.";
}
// If parameterDescriptor is null, this is an undeclared route parameter which only occurs
// when source is FromUri. Ignored in request model and among resource parameters but listed
// as a simple string here.
ModelDescription modelDescription = modelGenerator.GetOrCreateModelDescription(typeof(string));
AddParameterDescription(apiModel, apiParameter, modelDescription);
这将给你:
这些方法并不完美(我不喜欢修改生成的HelpPageConfigurationExtensions
)。但是似乎没有其他方法来抑制或填充version
参数的描述。
https://stackoverflow.com/questions/49341078
复制相似问题