原文:http://www.sharejs.com/codes/csharp/8603
public static NameValueCollection ParseUrl(string url, out string baseUrl)
{
baseUrl = "";
if (string.IsNullOrEmpty(url))
return null;
NameValueCollection nvc = new NameValueCollection();
int questionMarkIndex = url.IndexOf('?');
if (questionMarkIndex == -1)
baseUrl = url;
else
baseUrl = url.Substring(0, questionMarkIndex);
if (questionMarkIndex == url.Length - 1)
return null;
string ps = url.Substring(questionMarkIndex + 1);
// 开始分析参数对
System.Text.RegularExpressions.Regex re = new System.Text.RegularExpressions.Regex(@"(^|&)?(\w+)=([^&]+)(&|$)?", System.Text.RegularExpressions.RegexOptions.Compiled);
System.Text.RegularExpressions.MatchCollection mc = re.Matches(ps);
foreach (System.Text.RegularExpressions.Match m in mc)
{
nvc.Add(m.Result("$2").ToLower(), m.Result("$3"));
}
return nvc;
}