2

I have a controller action

    public class MyController : SitecoreController
        {
            [AllowCrossSiteJson]
            public JsonResult AddSubscriptionsJson(string pipeDelimitedMailingListIds, 
                                                       string email)
            {
                ...
            }
        }

I know i can get the action Url using

Url.Action("AddSubscriptionsJson","MyController")

Is there a way to get the available parameters through code? In say an array / list of string?

Like this?

["pipeDelimitedMailingListIds","email"]
tereško
  • 58,060
  • 25
  • 98
  • 150
aceanindita
  • 484
  • 1
  • 4
  • 11
  • It usually helps to add a tag of the programming language/framework you're using ;) – Nir Alfasi Jan 29 '15 at 08:24
  • You can try this http://stackoverflow.com/questions/214086/how-can-you-get-the-names-of-method-parameters . You need to write some helper method to do so but it is possible. – Devesh Jan 29 '15 at 08:40

2 Answers2

0

Perhaps not the "best" way of achieving this but here is goes anyway. This sample code assumes the GetParamNames() method inside the same controller as the action method you are trying to pick for parameter names.

private List<string> GetParamNames(System.Reflection.MethodInfo method)
{
    var output = new List<string>();

    if (method != null && method.GetParameters().Length > 0)
    {
        foreach(var par in method.GetParameters())
            output.Add(par.Name) ;
    }

    return output;
}       

and then from within the same controller you could call:

var parameters = GetParamNames(this.GetType().GetMethod("your action method name"));

the GetParamNames() method can obviously be placed in a more generic location like a shared utilities class for use by more than one class.

Hope this helps.

*Solution comes from a previous post ...names of method parameters

Community
  • 1
  • 1
Chris du Preez
  • 539
  • 3
  • 8
0

In addition to the information Chris posted, the following link has the required information to fetch the MethodInfo for MVC actions - using the controller / action name to resolve the method: How do I get the MethodInfo of an action, given action, controller and area names?

Community
  • 1
  • 1
aceanindita
  • 484
  • 1
  • 4
  • 11