简体   繁体   English

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

[英]WebAPI Help Page - Documentation for return or parameter model/class properties

I am using Web API Help Page with Web API 2 (5.0) - both the latest Nuget packages. 我正在使用Web API帮助页面与Web API 2(5.0) - 两个最新的Nuget包。 I would like the help documentation to show the comments of the properties on classes that are parameters or returned in the body of the HttpResponseMessage. 我希望帮助文档显示属性的注释,这些类是参数或在HttpResponseMessage的主体中返回。

For example, I have a controller method like this: 例如,我有一个像这样的控制器方法:

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

I would like the XML comments that I have on MyClassType1 and MyClassType2 to be displayed on the help page for the above post action. 我想在MyClassType1MyClassType2上的XML注释显示在上述帖子操作的帮助页面上。

Everywhere I have looked, so far it appears that this is not yet supported. 我看过的每个地方,到目前为止看来还没有支持。 However, I am wondering if anyone has been able to get this to work by extending ApiExplorer, adding to XmlDocumentationProvider, etc? 但是,我想知道是否有人能够通过扩展ApiExplorer,添加到XmlDocumentationProvider等来实现这一点?

I know that the comments and properties are included in the XML file that gets generated, so I could try to parse that manually (all the parameter and return types are in the MyAssemblyName.Models namespace, so my thought was I could look for the XML nodes that have a member name starting with that namespace. However, I know the built-in web API help pages have some caching features, so I prefer to somehow incorporate this with the existing functionality (just add on to it). 我知道注释和属性包含在生成的XML文件中,所以我可以尝试手动解析(所有参数和返回类型都在MyAssemblyName.Models命名空间中,所以我的想法是我可以查找XML具有以该命名空间开头的成员名称的节点。但是,我知道内置的Web API帮助页面具有一些缓存功能,所以我更喜欢以某种方式将其与现有功能相结合(只需添加它)。

I have managed to show the types of the parameters (one layer down only) by updating the Parameters.cshtml template to this: 我已经设法通过更新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))
            {
                <tr>
                    <td class="parameter-name"><b>@parameter.Name</b></td>
                    <td class="parameter-properties">
                        @foreach (PropertyInfo property in parameterType.GetProperties())
                        {
                            <text>@property.Name : @property.PropertyType.GetFriendlyTypeName()</text>
                            <br/>
                        }
                    </td>
                    <td class="parameter-documentation"><pre>@parameterDocumentation</pre></td>
                    <td class="parameter-source">
                        @switch(parameter.Source)
                        {
                            case ApiParameterSource.FromBody:
                                <p>Define this parameter in the request <b>body</b>.</p>
                                break;
                            case ApiParameterSource.FromUri:
                                <p>Define this parameter in the request <b>URI</b>.</p>
                                if (parameter.ParameterDescriptor.IsOptional)
                                {
                                    <p>This parameter is <b>optional</b>.</p>
                                }
                                break;
                            default:
                                <p>None.</p>
                                break;
                        }
                    </td>
                </tr>
            }
        }
    </tbody>
</table>

where the GetFriendlyTypeName() method above is implemented as shown here: How can I get the correct text definition of a generic type using reflection? 其中上面的GetFriendlyTypeName()方法实现如下所示: 如何使用反射获得泛型类型的正确文本定义?

However, this doesn't get me the comments from these classes and it doesn't help with nested types (eg if my model had a complex-typed property on it, it would not show the properties of that complex-typed property). 但是,这并没有让我得到这些类的注释,它对嵌套类型没有帮助(例如,如果我的模型上有一个复杂类型的属性,它就不会显示该复杂类型属性的属性)。 And the types are not useful enough without their XML comments anyway. 如果没有他们的XML注释,这些类型就没用了。

Also, this only applies to parameters, but not to return types contained in the body of the HttpResponseMessage. 此外,这仅适用于参数,但不适用于返回HttpResponseMessage正文中包含的类型。 I was able to get the response samples to work by implementing a ResponseTypeAttribute as shown here: Auto generated help pages with return type HttpResponseMessage but again that doesn't give me the properties with XML comments. 我能够通过实现ResponseTypeAttribute来获取响应示例,如下所示: 自动生成的帮助页面,其返回类型为HttpResponseMessage,但同样没有给出带有XML注释的属性。 I could use reflection to get the types similarly to how I got the parameter types again, but I really would like the XML comments together with types, and including nested complex types. 我可以使用反射来获取类型,类似于我再次获取参数类型的方式,但我真的希望XML注释与类型一起使用,并包括嵌套的复杂类型。

I would also find it acceptable to document the model/class documentation (properties with types and XML comments) separately from the service calls, and have the service calls just show the name of the type that they return (then at least the user could find the documentation for that type). 我还发现将模型/类文档(带有类型和XML注释的属性)与服务调用分开记录是可以接受的,并且让服务调用只显示它们返回的类型的名称(然后至少用户可以找到该类型的文档)。

Has anyone been able to implement something similar to what I am trying to do for either parameters or return types, preferably both? 有没有人能够实现类似于我试图为参数或返回类型做的事情,最好是两者兼而有之? Or any ideas to point me in the right direction? 还是有任何想法指出我正确的方向?

The feature of documenting individual properties of a complex type is currently in development and would be available for next release. 记录复杂类型的各个属性的功能目前正在开发中,可供下一版本使用。 That said you can currently get the HelpPage package from the Nightly builds and try it. 那说你现在可以从Nightly版本中获取HelpPage包并尝试它。 You should be able to upgrade this package onto an existing 5.0 web api project. 您应该能够将此包升级到现有的5.0 Web api项目。

Also currently we document only the action's input types and not the response types as generally users would want to know about the input types's information. 目前,我们仅记录操作的输入类型,而不是通常用户想要了解输入类型信息的响应类型。 But I can see that users might want the response type's information for consumption too...we will look into this and will let you know. 但我可以看到用户可能也希望响应类型的信息也可以用于消费......我们将调查此信息并告知您。

You can make modifications to allow html in documentation comments Here is a Reference . 您可以进行修改以允许在文档注释中使用html 这是一个参考

After you allow HTML you can put a table with property information where the XML documentation param tag description goes. 在允许HTML之后,您可以将包含属性信息的表放在XML文档参数标记描述中。 You can also look at a help page HTML source to get the class attribute names that are being used for help page tables so your table will use the same CSS as all the other tables. 您还可以查看帮助页面HTML源以获取用于帮助页面表的类属性名称,以便您的表将使用与所有其他表相同的CSS。

This is a hack but it works perfectly. 这是一个黑客但它的工作完美。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM