-1

i have a navigation page and i wanna that when click on create navigation i show dropdown that include of Controllers name and when choose one controller then in another dropdown show related action

so how can i find my controllers name and action ?

tereško
  • 58,060
  • 25
  • 98
  • 150
epj
  • 11
  • 2

1 Answers1

2

I did do the heavy lifting for the controllers and actions. This code generates an unordered list with list items and can be dropped in your view. You can use your favorite jquery menu plugin that turns this list into a menu with the desired effect.

This code can be easily refactored to a solution where a controller and a (partial)view is used in case you need to reuse this snippet.

<ul>
@{
    var allTypes = 
            from asm 
            in AppDomain.CurrentDomain.GetAssemblies()
            let types = (
                from type 
                in asm.GetTypes()
                let contname = type.Name.Replace("Controller","")
                let methods = (
                    from method 
                    in type.GetMethods()
                    let hasHttpPost =
                        method.GetCustomAttributes(
                            typeof(HttpPostAttribute),
                            false).Length > 0
                    where (method.ReturnType.IsSubclassOf(
                                    typeof(ActionResult))
                    || method.ReturnType == typeof(ActionResult))
                    && !hasHttpPost
                    select new
                    {
                        Controller = contname,
                        Action = method.Name
                    }
                )
                where type.IsSubclassOf(typeof(Controller))
                select new {Name = contname, Controllers = methods }
                )
            select new { asm.FullName, AllControllers = types };


    foreach (var controllers in allTypes)
    {
        foreach (var controller in controllers.AllControllers)
        {
            <li>
            @controller.Name
            <ul>
            @{
            foreach (var controlleraction in controller.Controllers)
            {
              <li>
                <a href="@controlleraction.Controller/@controlleraction.Action">
                  @controlleraction.Action
                </a>
              </li>
            }
            }
            </ul>
            </li>
        }
    }
}
</ul>
rene
  • 41,474
  • 78
  • 114
  • 152
  • tnx buddy it is so nice,so if i want to have actions that has view what happend,i mean i want to have list of action that has view ,what should i do ? – epj Jun 04 '12 at 12:00
  • You bascially can't because MVC uses convention over confiuration which means more or less that a Route to an action on a Controller results in a View. The above code snippet now returns every public method on a controller that returns something that inherits from ActionResult. You can restrict the where clause for method.ReturnType to only ViewResult (like so: where method.ReturnType == typeof(ViewResult)) but if that still gives you the wrong results I would advice to implement a check against an exclusion list. That list needs to be maintained by hand. – rene Jun 05 '12 at 18:44