There is no standard way to "replace dictionary values" in .NET. Although there are several template engines, it is not so difficult to write a small solution for such an operation. Here is an example that works in LINQPad and uses a Regular Expression with conformance assessment .
As a result of the URL, it is the responsibility of the callers to ensure that all the values ββprovided are correctly encoded . I recommend using Uri.EscapeDataString if necessary .. but do not loop if it is being processed elsewhere.
In addition, the rules of what to do when no replacement is found should be tailored to your needs. If not found replacements should be completely excluded along with the query string key, the following can extend the regular expression to @"\w+=({\w+})" to also capture the parameter key in this particular template situation.
string ReplaceUsingDictionary (string src, IDictionary<string, object> replacements) { return Regex.Replace(src, @"{(\w+)}", (m) => { object replacement; var key = m.Groups[1].Value; if (replacements.TryGetValue(key, out replacement)) { return Convert.ToString(replacement); } else { return m.Groups[0].Value; } }); } void Main() { var replacements = new Dictionary<string, object> { { "networkid", "WHEEE!!" }
More advanced methods, such as reflection and transformations, are possible, but they should be left for real template engines.
user2864740 Nov 29 '13 at 5:15 2013-11-29 05:15
source share