The route I received here was a reflection and a Dictionary containing all valid actions in the corresponding Controller, which is stored in Appendix []. The actual action is determined by checking the ReturnType method and verifying that it (or comes from) the ActionResult and that it is not private. I could do a few more checks, but for now there are enough of them.
public static bool MethodIsAction(MethodInfo method) { if (method == null) throw new ArgumentNullException("Invalid Parameter: method cannot be null."); if (method.ReturnType != typeof(ActionResult) && method.ReturnType.BaseType != typeof(ActionResult)) return false; if (method.IsPrivate) return false; return true; }
The action dictionary is built with the following method inside Application_Start:
public static Dictionary<string, MethodInfo> GetActionDictionary(Type controller) { Dictionary<string, MethodInfo> dict = null; var methods = controller.GetMethods().Where(MethodIsAction); if (methods.Any()) { dict = new Dictionary<string, MethodInfo>(StringComparer.OrdinalIgnoreCase); foreach (var action in methods) dict.Add(action.Name, action); } return dict; }
When a user requests a qualifying action, I simply specify the name of the action in the dictionary, and if for this action name I call the MethodInfo method, I call it. Although it still requires reflection, it is at least optimized so that it only ever happens while the application is running.
source share