且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Web API帮助页面 - 自定义属性文档

更新时间:2023-02-12 08:24:54

这应该是@Josh答案的补充。如果您不仅要从模型类列出属性,还要包括每个属性的文档,那么应该修改 Areas / HelpPage / XmlDocumentationProvider.cs 文件如下:

  public virtual string GetDocumentation(HttpParameterDescriptor parameterDescriptor)
{
ReflectedHttpParameterDescriptor reflectParameterDescriptor = parameterDescriptor as ReflectedHttpParameterDescriptor;
if(reflectParameterDescriptor!= null)
{
if(reflectParameterDescriptor.ParameterInfo为CustomParameterInfo)
{
const string PropertyExpression =/ doc / members / member [@命名= 'P:{0}'];
var pi =(CustomParameterInfo)reflectParameterDescriptor.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(reflectParameterDescriptor.ActionDescriptor);
if(methodNode!= null)
{
string parameterName = reflectionParameterDescriptor.ParameterInfo.Name;
XPathNavigator parameterNode = methodNode.SelectSingleNode(String.Format(CultureInfo.InvariantCulture,ParameterExpression,parameterName));
if(parameterNode!= null)
{
return parameterNode.Value.Trim();
}
}
}
}

返回null;
}

CustomParameterInfo 类应该保留属性信息:

 内部类CustomParameterInfo:ParameterInfo 
{
public PropertyInfo Prop {get;私人集合

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


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)

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.

Parameters

Name | Description | Additional information

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

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

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;
}

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;
    }
}