// Associates the action method argument to the corresponding resource property // used to populate the argument. This is used when sending the route-info to // ASP.NET Core to resolve the corresponding URL. private void SetRouteValues(LambdaExpression expression) { // The specified controller action declared method parameters: var controllerAction = (MethodCallExpression)expression.Body; var actionParams = controllerAction.Method.GetParameters() .OrderBy(p => p.Position) .ToArray(); for (int i = 0; i < controllerAction.Arguments.Count(); i++) { // The input argument to the action method parameter: var passedArg = controllerAction.Arguments[i]; if (passedArg is MemberExpression propExpArg) { var propInfo = (PropertyInfo)propExpArg.Member; var actionParam = actionParams[i]; var routeValue = new ActionParamValue(actionParam.Name, propInfo); _link.AddRouteValue(routeValue); continue; } // This is the case when the action parameter specifies a default value. if (passedArg is UnaryExpression unaryExpArg) { var operand = (MemberExpression)unaryExpArg.Operand; var propInfo = (PropertyInfo)operand.Member; var actionParam = actionParams[i]; var routeValue = new ActionParamValue(actionParam.Name, propInfo); _link.AddRouteValue(routeValue); } } }
public ActionUrlLink(ActionParamValue actionParamValues) { if (actionParamValues == null) { throw new ArgumentNullException(nameof(actionParamValues), "Action parameter values not specified."); } _routeValues = new List <ActionParamValue> { actionParamValues }; RouteValues = _routeValues.ToReadOnly(); }
/// <summary> /// Adds a mapping indicating the resources's property corresponding to an action parameter. /// </summary> /// <param name="actionParam">Mapping between action parameter and corresponding resource property.</param> public void AddRouteValue(ActionParamValue actionParam) { if (actionParam == null) { throw new ArgumentNullException(nameof(actionParam), "Route parameter value not specified."); } if (_routeValues.Any(rv => rv.ActionParamName == actionParam.ActionParamName)) { throw new ArgumentException($"The action parameter named: {actionParam.ActionParamName} has already been added."); } _routeValues.Add(actionParam); }