[英]ASP.Net WebApi 2 sample text attribute
Is there a way do provide samples for the web api help pages generation using attributes? 有没有办法为使用属性的web api帮助页面生成提供样本? I know that I can provide samples by going to /Areas/HelpPage/... but I want them all in one place with my code. 我知道我可以通过转到/ Areas / HelpPage / ...来提供样本,但是我希望它们都能在我的代码中放在一个地方。
Something along these lines: 这些方面的东西:
/// <summary>
/// userPrincipalName attribute of the user in AD
/// </summary>
[TextSample("john.smith@contoso.com")]
public string UserPrincipalName;
This could be achieved by creating a custom attribute yourself, something like: 这可以通过自己创建自定义属性来实现,例如:
[AttributeUsage(AttributeTargets.Property)]
public class TextSampleAttribute : Attribute
{
public string Value { get; set; }
public TextSampleAttribute(string value)
{
Value = value;
}
}
And then modifying the SetPublicProperties
method of ObjectGenerator
like this: 然后修改SetPublicProperties
的方法ObjectGenerator
这样的:
private static void SetPublicProperties(Type type, object obj, Dictionary<Type, object> createdObjectReferences)
{
PropertyInfo[] properties = type.GetProperties(BindingFlags.Public | BindingFlags.Instance);
ObjectGenerator objectGenerator = new ObjectGenerator();
foreach (PropertyInfo property in properties)
{
if (property.IsDefined(typeof (TextSampleAttribute), false))
{
object propertyValue = property.GetCustomAttribute<TextSampleAttribute>().Value;
property.SetValue(obj, propertyValue, null);
}
else if (property.CanWrite)
{
object propertyValue = objectGenerator.GenerateObject(property.PropertyType, createdObjectReferences);
property.SetValue(obj, propertyValue, null);
}
}
}
I've added a check to see if the TextSampleAttribute is defined and if so use it's value instead of the auto-generated one. 我添加了一个检查以查看TextSampleAttribute是否已定义,如果是,则使用它的值而不是自动生成的值。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.