1

我有一个这样的 ASP.Net Web Api 控制器:

public class SomeController : ApiController
{
    public myObject Post([FromUri]string qsVar, [FromBody]yourObject bVar)
    {
        return myObject(qsVar, bVar);
    }
}

我正在编写一个文档生成器,需要确定一个参数是[FromUri]还是[FromBody]基于它的ParameterInfo.

Type tc = typeof(SomeController);

foreach (MethodInfo m in tc.GetMethods())
{
    foreach (ParameterInfo p in m.GetParameters())
    {
        if (p.isFromBody ???) doThis(); else doThat();
    }
}

如何确定ASP.Net Web Api 控制器方法中的参数是否具有[FromUri]或标志?[FromBody]

回答:

bool isFromUri = p.GetCustomAttributes(false)
    .Any(x => x.GetType() == typeof(FromUriAttribute));
4

1 回答 1

0

你可以看看CustomAttributes楼盘:

bool hasFromBodyAttribute = p
    .CustomAttributes
    .Any(x => x.AttributeType == typeof(FromBodyAttribute));

if (hasFromBodyAttribute) 
{
    doThis(); 
}
else 
{
    doThat();
}
于 2013-07-07T18:02:16.860 回答