我需要單元測試我的POST動作方法,所以我需要它們的列表。我正在使用反射來找到[AcceptVerbs(HttpVerbs.Post)]
的那些方法。如何獲取控制器的POST操作方法?
// get controller's methods
typeof(FooController).GetMethods()
// get controller's action methods
.Where(q => q.IsPublic && q.IsVirtual && q.ReturnType == typeof(ActionResult))
// get actions decorated with AcceptVerbsAttribute
.Where(q => q.CustomAttributes
.Any(w => (w.AttributeType == _typeof(AcceptVerbsAttribute)))
)
// ...everything is ok till here...
// filter for those with the HttpVerbs.Post ctor arg
.Where(q => q.CustomAttributes
.Any(w => w.ConstructorArguments.Any(e => e.Value.Equals(HttpVerbs.Post))))
;
然而,這給了我一個空的列表。問題在於最後一次檢查屬性的ctor。我如何解決它?
值得注意的是,有兩種方法可以將操作的方法聲明爲POST:使用上述AcceptVerbsAttribute
和HttpPostAttribute
。
如上所述,這似乎是一個觀察而不是一個問題。 – neontapir