PageRenderTime 84ms CodeModel.GetById 19ms RepoModel.GetById 1ms app.codeStats 0ms

/mcs/class/System.Web.Mvc/System.Web.Mvc/ActionMethodSelector.cs

https://bitbucket.org/steenlund/mono-2.6.7-for-amiga
C# | 125 lines | 86 code | 22 blank | 17 comment | 5 complexity | eac19e8026324672d64301fe1dc2ddf9 MD5 | raw file
Possible License(s): LGPL-2.0, MPL-2.0-no-copyleft-exception, CC-BY-SA-3.0, GPL-2.0, LGPL-2.1
  1. /* ****************************************************************************
  2. *
  3. * Copyright (c) Microsoft Corporation. All rights reserved.
  4. *
  5. * This software is subject to the Microsoft Public License (Ms-PL).
  6. * A copy of the license can be found in the license.htm file included
  7. * in this distribution.
  8. *
  9. * You must not remove this notice, or any other, from this software.
  10. *
  11. * ***************************************************************************/
  12. namespace System.Web.Mvc {
  13. using System;
  14. using System.Collections.Generic;
  15. using System.Globalization;
  16. using System.Linq;
  17. using System.Reflection;
  18. using System.Text;
  19. using System.Web.Mvc.Resources;
  20. internal sealed class ActionMethodSelector {
  21. public ActionMethodSelector(Type controllerType) {
  22. ControllerType = controllerType;
  23. PopulateLookupTables();
  24. }
  25. public Type ControllerType {
  26. get;
  27. private set;
  28. }
  29. public MethodInfo[] AliasedMethods {
  30. get;
  31. private set;
  32. }
  33. public ILookup<string, MethodInfo> NonAliasedMethods {
  34. get;
  35. private set;
  36. }
  37. private AmbiguousMatchException CreateAmbiguousMatchException(List<MethodInfo> ambiguousMethods, string actionName) {
  38. StringBuilder exceptionMessageBuilder = new StringBuilder();
  39. foreach (MethodInfo methodInfo in ambiguousMethods) {
  40. string controllerAction = Convert.ToString(methodInfo, CultureInfo.CurrentUICulture);
  41. string controllerType = methodInfo.DeclaringType.FullName;
  42. exceptionMessageBuilder.AppendLine();
  43. exceptionMessageBuilder.AppendFormat(CultureInfo.CurrentUICulture, MvcResources.ActionMethodSelector_AmbiguousMatchType, controllerAction, controllerType);
  44. }
  45. string message = String.Format(CultureInfo.CurrentUICulture, MvcResources.ActionMethodSelector_AmbiguousMatch,
  46. actionName, ControllerType.Name, exceptionMessageBuilder);
  47. return new AmbiguousMatchException(message);
  48. }
  49. public MethodInfo FindActionMethod(ControllerContext controllerContext, string actionName) {
  50. List<MethodInfo> methodsMatchingName = GetMatchingAliasedMethods(controllerContext, actionName);
  51. methodsMatchingName.AddRange(NonAliasedMethods[actionName]);
  52. List<MethodInfo> finalMethods = RunSelectionFilters(controllerContext, methodsMatchingName);
  53. switch (finalMethods.Count) {
  54. case 0:
  55. return null;
  56. case 1:
  57. return finalMethods[0];
  58. default:
  59. throw CreateAmbiguousMatchException(finalMethods, actionName);
  60. }
  61. }
  62. internal List<MethodInfo> GetMatchingAliasedMethods(ControllerContext controllerContext, string actionName) {
  63. // find all aliased methods which are opting in to this request
  64. // to opt in, all attributes defined on the method must return true
  65. var methods = from methodInfo in AliasedMethods
  66. let attrs = (ActionNameSelectorAttribute[])methodInfo.GetCustomAttributes(typeof(ActionNameSelectorAttribute), true /* inherit */)
  67. where attrs.All(attr => attr.IsValidName(controllerContext, actionName, methodInfo))
  68. select methodInfo;
  69. return methods.ToList();
  70. }
  71. private static bool IsMethodDecoratedWithAliasingAttribute(MethodInfo methodInfo) {
  72. return methodInfo.IsDefined(typeof(ActionNameSelectorAttribute), true /* inherit */);
  73. }
  74. private static bool IsValidActionMethod(MethodInfo methodInfo) {
  75. return !(methodInfo.IsSpecialName ||
  76. methodInfo.GetBaseDefinition().DeclaringType.IsAssignableFrom(typeof(Controller)));
  77. }
  78. private void PopulateLookupTables() {
  79. MethodInfo[] allMethods = ControllerType.GetMethods(BindingFlags.InvokeMethod | BindingFlags.Instance | BindingFlags.Public);
  80. MethodInfo[] actionMethods = Array.FindAll(allMethods, IsValidActionMethod);
  81. AliasedMethods = Array.FindAll(actionMethods, IsMethodDecoratedWithAliasingAttribute);
  82. NonAliasedMethods = actionMethods.Except(AliasedMethods).ToLookup(method => method.Name, StringComparer.OrdinalIgnoreCase);
  83. }
  84. private static List<MethodInfo> RunSelectionFilters(ControllerContext controllerContext, List<MethodInfo> methodInfos) {
  85. // remove all methods which are opting out of this request
  86. // to opt out, at least one attribute defined on the method must return false
  87. List<MethodInfo> matchesWithSelectionAttributes = new List<MethodInfo>();
  88. List<MethodInfo> matchesWithoutSelectionAttributes = new List<MethodInfo>();
  89. foreach (MethodInfo methodInfo in methodInfos) {
  90. ActionMethodSelectorAttribute[] attrs = (ActionMethodSelectorAttribute[])methodInfo.GetCustomAttributes(typeof(ActionMethodSelectorAttribute), true /* inherit */);
  91. if (attrs.Length == 0) {
  92. matchesWithoutSelectionAttributes.Add(methodInfo);
  93. }
  94. else if (attrs.All(attr => attr.IsValidForRequest(controllerContext, methodInfo))) {
  95. matchesWithSelectionAttributes.Add(methodInfo);
  96. }
  97. }
  98. // if a matching action method had a selection attribute, consider it more specific than a matching action method
  99. // without a selection attribute
  100. return (matchesWithSelectionAttributes.Count > 0) ? matchesWithSelectionAttributes : matchesWithoutSelectionAttributes;
  101. }
  102. }
  103. }