相关疑难解决方法(0)

WebAPI帮助页面-返回或参数模型/类属性的文档

我正在将Web API帮助页面与Web API 2(5.0)一起使用-均为最新的Nuget软件包。我希望帮助文档在HttpResponseMessage的正文中显示作为参数或返回的类的属性的注释。

例如,我有一个像这样的控制器方法:

public HttpResponseMessage Post([FromBody] MyClassType1 myClass)
{
    // Business logic removed for clarity
    return Request.CreateResponse(HttpStatusCode.OK, new MyClassType2());
}
Run Code Online (Sandbox Code Playgroud)

我想要上面发布操作的XML注释,MyClassType1并将MyClassType2其显示在帮助页面上。

我看过的所有地方,到目前为止似乎还不支持。但是,我想知道是否有人能够通过扩展ApiExplorer,添加到XmlDocumentationProvider等使它正常工作?

我知道注释和属性包含在生成的XML文件中,因此我可以尝试手动解析(所有参数和返回类型都在MyAssemblyName.Models名称空间中,因此我想我可以查找具有但是,我知道内置的Web API帮助页面具有某些缓存功能,因此,我宁愿以某种方式将其与现有功能合并(只需对其进行添加)。

通过将Parameters.cshtml模板更新为以下内容,我设法显示了参数的类型(仅向下一层):

@using System.Reflection
@using System.Threading
@using System.Web.Http.Description
@using Regency.API.Services.Areas.HelpPage
@model System.Collections.ObjectModel.Collection<ApiParameterDescription>

<table class="help-page-table">
    <thead>
        <tr><th>Name</th><th>Properties</th><th>Description</th><th>Additional information</th></tr>
    </thead>
    <tbody>
        @foreach (ApiParameterDescription parameter in Model)
        {
            string parameterDocumentation = parameter.Documentation ?? "No documentation available.";
            Type parameterType = parameter.ParameterDescriptor.ParameterType;

            // Don't show CancellationToken because it's a special parameter
            if (!typeof (CancellationToken).IsAssignableFrom(parameter.ParameterDescriptor.ParameterType)) …
Run Code Online (Sandbox Code Playgroud)

c# asp.net-mvc asp.net-web-api asp.net-mvc-apiexplorer asp.net-web-api-helppages

4
推荐指数
1
解决办法
5628
查看次数