Best way to check upper and lower case query strings

I have a problem when I need to extract a query string parameter from a url. The parameter can be either "Territory" or "Territory" or other variants of the upper and lower case of this word. Although the following works in the first two cases, I wonder if there is a better way?

 IDictionary<string, string> queryString = HtmlPage.Document.QueryString; if (queryString.ContainsKey("territory")) { ish.SetDefaultRegion(int.Parse(queryString["territory"])); // do something (the same something as below) } else if (queryString.ContainsKey("Territory")) { ish.SetDefaultRegion(int.Parse(queryString["Territory"])); // do something (the same something as above) } 

I would prefer to insert a query string into a case-insensitive dictionary (ie if the user accidentally typed "Territory" , this code will not work, since I can just check that the word exists regardless of the cover?

+7
source share
3 answers

Use the dictionary with a case-insensitive key:

 var queryParams = new Dictionary<string, string>( HtmlPage.Document.QueryString, StringComparer.InvariantCultureIgnoreCase ); if (queryParams.ContainsKey("territory")) { // Covers "territory", "Territory", "teRRitory", etc. } 
+14
source

If your QueryString object is not a dictionary object, but instead is something like NameValueCollection ...

 IDictionary<string, string> queryString = QueryString.AllKeys.ToDictionary(k => k.ToLowerInvariant(), k => QueryString[k]); 
+2
source

Hello, it could also be a TeRriTory, in querystring do .Tolower ()

0
source

All Articles