コード例 #1
0
        public string Parse(string input, IEnumerable <Match> matches)
        {
            var enumerable = matches as Match[] ?? matches.ToArray();

            if (!enumerable.Any())
            {
                return(input);
            }

            ValueTuple <string, StringValues>[] formValues;
            try
            {
                // We don't care about any exceptions here.
                formValues = _httpContextService.GetFormValues();
            }
            catch
            {
                formValues = new ValueTuple <string, StringValues> [0];
            }

            // TODO there can be multiple form values, so this should be fixed in the future.
            var formDict = formValues.ToDictionary(f => f.Item1, f => f.Item2.First());

            foreach (var match in enumerable)
            {
                if (match.Groups.Count != 3)
                {
                    continue;
                }

                var formValueName = match.Groups[2].Value;
                formDict.TryGetValue(formValueName, out var replaceValue);

                input = input.Replace(match.Value, replaceValue);
            }

            return(input);
        }