Example #1
0
        private IReadOnlyCollection <ActionInfo> GetActionInfos(Type controllerType)
        {
            var actions = new Dictionary <string, ActionInfo>(StringComparer.OrdinalIgnoreCase);

            foreach (var method in controllerType.GetMethods(
                         BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Static))
            {
                if (IsActionMethod(method))
                {
                    string actionName    = GetActionName(method);
                    string actionUrlName = GetUrlName(method);
                    var    actionInfo    = new ActionInfo(actionName, actionUrlName);

                    // Check if there is already an action with the same name
                    if (actions.TryGetValue(actionName, out ActionInfo action))
                    {
                        if (action.UrlName == null)
                        {
                            actions[actionName] = action;
                        }
                        else if (!string.Equals(action.UrlName, actionName, StringComparison.OrdinalIgnoreCase))
                        {
                            throw new InvalidOperationException(
                                      $"The '{controllerType}' type has multiple actions with the name of '{actionName}', " +
                                      $"but they have different URL names specified.");
                        }
                    }
                    else
                    {
                        actions.Add(actionName, actionInfo);
                    }
                }
            }

            return(actions.Values);
        }
 /// <summary>
 /// Gets the localized name of the given <paramref name="action"/>.
 /// </summary>
 /// <param name="controller">The controller of the <paramref name="action"/>.</param>
 /// <param name="action">The action to be localized.</param>
 /// <returns>The localized name of the <paramref name="action"/>.</returns>
 protected abstract string GetLocalizationForAction(ControllerInfo controller, ActionInfo action);
 /// <summary>
 /// Gets the lowercase, hyphen-separated name of the <paramref name="action"/>.
 /// </summary>
 /// <param name="controller">The controller of the <paramref name="action"/>.</param>
 /// <param name="action">The action to be localized.</param>
 /// <returns>The lowercase, hyphen-separated name of the <paramref name="action"/>,
 /// or <see cref="UrlNameAttribute.Name"/>'s value, if present, and <see cref="IgnoreUrlNameAttribute"/>
 /// is set to <see langword="false"/>.</returns>
 protected override string GetLocalizationForAction(ControllerInfo controller, ActionInfo action)
 {
     if (action.UrlName != null && !IgnoreUrlNameAttribute)
     {
         return(action.UrlName);
     }
     else
     {
         return(PascalToKebabCase(action.Name));
     }
 }
 /// <summary>
 /// Gets the localized name of the given <paramref name="action"/> based on the <see cref="UrlNameAttribute.Name"/> value.
 /// </summary>
 /// <param name="controller">The controller of the <paramref name="action"/>.</param>
 /// <param name="action">The action to be localized.</param>
 /// <returns>The <see cref="UrlNameAttribute.Name"/>'s value, if the attribute is present;
 /// otherwise the <see cref="ActionInfo.Name"/>'s value.</returns>
 protected override string GetLocalizationForAction(ControllerInfo controller, ActionInfo action)
 {
     return(action.UrlName ?? action.Name);
 }