/// <summary> /// Replace a variable in the <c>RouteFormat</c> with a specified value. /// </summary> /// <param name="variableName">The variable name to replace.</param> /// <param name="variableValue">The value to replace with.</param> /// <param name="workingRoute">An 'in progress' route that may contain values that have already been replaced.</param> /// <returns>A <c>workingRoute</c></returns> public string SetVariable(string variableName, string variableValue, string workingRoute = null) { if (!variableName.StartsWith(VariableStartChar.ToString()) && !variableName.EndsWith(VariableEndChar.ToString())) { variableName = string.Format("{1}{0}{2}", variableName, VariableStartChar, VariableEndChar); } if (!string.IsNullOrEmpty(workingRoute)) { return(workingRoute.Replace(variableName, variableValue)); } else { return(RouteFormat.Replace(variableName, variableValue)); } }
/// <summary> /// Extract variable values from a given instance of the route you're trying to parse. /// </summary> /// <param name="routeInstance">The route instance.</param> /// <returns>A dictionary of Variable names mapped to values.</returns> public Dictionary <string, string> ParseRouteInstance(string routeInstance) { var inputValues = new Dictionary <string, string>(); string formatUrl = new string(RouteFormat.ToArray()); foreach (string variable in Variables) { formatUrl = formatUrl.Replace(WrapWithVariableChars(variable), string.Format(VariableTokenPattern, variable)); } var regex = new Regex(formatUrl, RegexOptions.IgnoreCase); var matchCollection = regex.Match(routeInstance); foreach (var variable in Variables) { var value = ((Group)matchCollection.Groups[variable]).Value; inputValues.Add(variable, value); } return(inputValues); }