Created
July 3, 2012 13:26
-
-
Save hvitorino/3039690 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public class ActionInvoker : ControllerActionInvoker | |
| { | |
| protected override ActionDescriptor FindAction(ControllerContext controllerContext, ControllerDescriptor controllerDescriptor, string actionName) | |
| { | |
| var nome = ExtractActionName(actionName); | |
| var method = controllerDescriptor.ControllerType.GetMethod(nome, BindingFlags.Public | BindingFlags.Instance); | |
| return new ReflectedActionDescriptor(method, nome, controllerDescriptor); | |
| } | |
| private static string ExtractActionName(string actionName) | |
| { | |
| var palavras = actionName.Split(new char[] { '-' }); | |
| var nome = string.Empty; | |
| foreach (var palavra in palavras) | |
| nome += palavra[0].ToString().ToUpper() + palavra.Substring(1); | |
| return nome; | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public class ControllerFactory : DefaultControllerFactory | |
| { | |
| public override IController CreateController(RequestContext requestContext, string controllerName) | |
| { | |
| Controller controllerInstance = null; | |
| var controllerType = GetControllerType(controllerName); | |
| controllerInstance = base.GetControllerInstance(requestContext, controllerType) as Controller; | |
| controllerInstance.ActionInvoker = new ActionInvoker(); | |
| return controllerInstance; | |
| } | |
| private Type GetControllerType(string controllerName) | |
| { | |
| var extractedName = ExtractControllerName(controllerName); | |
| var controllerType = this.GetType().Assembly.GetTypes() | |
| .Where(type => type.Name.ToLower() == extractedName.ToLower() && type.IsSubclassOf(typeof(Controller))) | |
| .FirstOrDefault(); | |
| return controllerType; | |
| } | |
| private static string ExtractControllerName(string controllerName) | |
| { | |
| var words = controllerName.Split(new char[] { '-' }); | |
| var extractedName = string.Empty; | |
| foreach (var word in words) | |
| extractedName += word[0].ToString().ToUpper() + word.Substring(1); | |
| return extractedName; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment