I want to count how many query string key appear in a string of URL. The URL here is an string so I can't use Request.QueryString.AllKeys to count how many key in the url. Currently, I have an solution for this by analyze the structure of url string and using count string within a string to count query string keys in an url string. Everyone can look clearly in my sample of code:
public int CountQueryStringKey(string urlString)
{
string urlWithoutKey = urlString.Substring(0, urlString.IndexOf("?"));
string allKeyString = urlString.Substring(urlString.IndexOf("?") + 1);
string[] allKeyAndValue = allKeyString.Split('&');
return allKeyAndValue.Length;
}
It is simple but not enough. What will happen if there is no query string key in string of url, and there are always different kind of url which I'm not sure it's structure.
I need some help for a good solution in this issue.