Я уже давно борюсь с этим вопросом и считаю, что нашел решение, которое должно работать большую часть времени. Это включает получение ControllerDescriptor
для рассматриваемого контроллера, а затем проверку каждого ActionDescriptor
, возвращаемого ControllerDescriptor.GetCanonicalActions()
.
Я закончил тем, что сделал действие, которое вернуло частичное представление в моем контроллере, но я думаю, что довольно легко выяснить, что происходит, поэтому не стесняйтесь брать код и изменять его в соответствии с вашими потребностями.
[ChildActionOnly]
public ActionResult Navigation()
{
// List of links
List<string> NavItems = new List<string>();
// Get a descriptor of this controller
ReflectedControllerDescriptor controllerDesc = new ReflectedControllerDescriptor(this.GetType());
// Look at each action in the controller
foreach (ActionDescriptor action in controllerDesc.GetCanonicalActions())
{
bool validAction = true;
// Get any attributes (filters) on the action
object[] attributes = action.GetCustomAttributes(false);
// Look at each attribute
foreach (object filter in attributes)
{
// Can we navigate to the action?
if (filter is HttpPostAttribute || filter is ChildActionOnlyAttribute)
{
validAction = false;
break;
}
}
// Add the action to the list if it's "valid"
if (validAction)
NavItems.Add(action.ActionName);
}
return PartialView(NavItems);
}
Возможно, есть и другие фильтры, но пока это соответствует моим потребностям.