2013-07-07 13 views
1

我有一個ASP.Net網頁API控制器是這樣的:確定參數位置從的ParameterInfo在網頁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(); 
    } 
} 

如何確定一個參數是否具有[FromUri][FromBody]標誌一個ASP.Net網頁API控制方法?

答:

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

我建議你看一看的ApiExplorer類:HTTP://博客。 msdn.com/b/yaohuang1/archive/2012/05/13/asp-net-web-api-introducing-iapiexplorer-apiexplorer.aspx。它應該爲您提供生成文檔所需的一切。 Web API還附帶了幫助頁面(如果適用):http://blogs.msdn.com/b/yaohuang1/archive/2012/08/15/introducing-the-asp-net-web-api-help-頁面preview.aspx。 –

+0

感謝@YoussefMoussaoui,我最初考慮到它有點吸引人的幫助頁面。我決定使用Swagger,我需要更多的控制過程來生成Swagger需要的json:https://developers.helloreverb.com/swagger/ – Greg

+0

嗨@Greg請看看我的問題=> http:// stackoverflow。 COM /問題/ 25206833 /列表的控制器和類功能於ASP網的Web-API –

回答

0

你可以看看在CustomAttributes屬性:

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

if (hasFromBodyAttribute) 
{ 
    doThis(); 
} 
else 
{ 
    doThat(); 
}