网页API帮助页面 - 自定义属性文档 [英] Web API Help pages - customizing Property documentation

查看:165
本文介绍了网页API帮助页面 - 自定义属性文档的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有我的web API和我加入了网页API帮助页面来自动生成我的文档。它是在我的参数都列了出来的方法伟大的工作,但我有一个像这样的方法:

I have my web api and I added the web api help pages to auto-generate my documentation. It's working great for methods where my parameters are listed out, but I have a method like this:

public SessionResult PostLogin(CreateSessionCommand request)

和我的帮助页面,它只是列出了属性部分的命令参数。然而,在样本请求部分,它列出了所有的属性我 CreateSessionCommand 类。

And, on my help page, it is only listing the command parameter in the properties section. However, in the sample request section, it lists out all of the properties of my CreateSessionCommand class.

参数

名称|说明|其他信息

请求|没有可用的文档。 |定义在请求主体此参数。

request | No documentation available. | Define this parameter in the request body.

我想它,而不是列出所有属性在我的 CreateSessionCommand 类。有没有一种简单的方法来做到这一点?

I would like it instead to list all of the properties in my CreateSessionCommand class. Is there an easy way to do this?

推荐答案

这应该作为一个除了@Josh答案。如果你想不仅列出从模型类的属性,而且还包括为每个属性文件,的区/ HelpPage / XmlDocumentationProvider.cs 的如下文件应该被修改:

this should go as an addition to @Josh answer. If you want not only to list properties from the model class, but also include documentation for each property, Areas/HelpPage/XmlDocumentationProvider.cs file should be modified as follows:

public virtual string GetDocumentation(HttpParameterDescriptor parameterDescriptor)
{
    ReflectedHttpParameterDescriptor reflectedParameterDescriptor = parameterDescriptor as ReflectedHttpParameterDescriptor;
    if (reflectedParameterDescriptor != null)
    {
        if (reflectedParameterDescriptor.ParameterInfo is CustomParameterInfo)
        {
            const string PropertyExpression = "/doc/members/member[@name='P:{0}']";
            var pi = (CustomParameterInfo) reflectedParameterDescriptor.ParameterInfo;

            string selectExpression = String.Format(CultureInfo.InvariantCulture, PropertyExpression, pi.Prop.DeclaringType.FullName + "." + pi.Prop.Name); 
            XPathNavigator methodNode = _documentNavigator.SelectSingleNode(selectExpression);
            if (methodNode != null)
            {
                return methodNode.Value.Trim();
            }
        }
        else
        {
            XPathNavigator methodNode = GetMethodNode(reflectedParameterDescriptor.ActionDescriptor);
            if (methodNode != null)
            {
                string parameterName = reflectedParameterDescriptor.ParameterInfo.Name;
                XPathNavigator parameterNode = methodNode.SelectSingleNode(String.Format(CultureInfo.InvariantCulture, ParameterExpression, parameterName));
                if (parameterNode != null)
                {
                    return parameterNode.Value.Trim();
                }
            }                    
        }
    }

    return null;
}

CustomParameterInfo 的类要保持财产信息,以及:

and CustomParameterInfo class should keep property info as well:

internal class CustomParameterInfo : ParameterInfo
{
    public PropertyInfo Prop { get; private set; }

    public CustomParameterInfo(PropertyInfo prop)
    {
        Prop = prop;
        base.NameImpl = prop.Name;
    }
}

这篇关于网页API帮助页面 - 自定义属性文档的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆