从.NET中的字符串中获取URL参数,可以使用以下方法:
HttpUtility.ParseQueryString
方法解析URL中的查询字符串。string url = "https://example.com/?param1=value1¶m2=value2";
string queryString = url.Split('?')[1];
var queryParams = HttpUtility.ParseQueryString(queryString);
string param1 = queryParams["param1"];
string param2 = queryParams["param2"];
string url = "https://example.com/?param1=value1¶m2=value2";
string pattern = @"(?<=\?|&)([^=&]*)=([^=&]*)";
var matches = Regex.Matches(url, pattern);
Dictionary<string, string> queryParams = new Dictionary<string, string>();
foreach (Match match in matches)
{
queryParams[match.Groups[1].Value] = match.Groups[2].Value;
}
string param1 = queryParams["param1"];
string param2 = queryParams["param2"];
这两种方法都可以从URL中获取查询字符串参数,并将其存储在字典中以便后续使用。
领取专属 10元无门槛券
手把手带您无忧上云