So, making an assumption that do not have / in them:
var splitVals = queryString.Split('/'); var vals = new Dictionary<string, string>(); for (int i = 2; i <= splitVals.Count; i++) { vals.Add(string.Format("param{0}", i), vals[i]); }
This will get you started. Now, if you want to set them to real variables, you will need to do some reflection and use reflection, but your question is not close enough to make any real assumptions.
EDIT
To make this code reusable, I would build an extension method:
namespace System { public static class StringExtensions { public static Dictionary<string, string> SplitQueryString(this string queryString) { var splitVals = queryString.Split('/'); var vals = new Dictionary<string, string>(); for (int i = 2; i <= splitVals.Count; i++) { vals.Add(string.Format("param{0}", i), vals[i]); } return vals; } } }
because then you can do this:
var vals = queryString.SplitQueryString();
Mike perrenoud
source share