Search code examples
c#namevaluecollection

NameValueCollection.AllKeys.Contains always returns false


System.Web.HttpContext.Current.Request.Params.AllKeys.Contains(key)

always returns false, but

System.Web.HttpContext.Current.Request.Params[key]

returns the value of the key.

Could someone tell me why this is happening?

In my case, the key is "ctl00$ContentPlaceHolder1$username", I debugged and checked that in both System.Web.HttpContext.Current.Request.Params.AllKeys and System.Web.HttpContext.Current.Request.Params, the exact key existed.


Solution

  • The Params collection is initialized with a case-insensitive string comparer. When you use Contains you're not providing any comparer, so you're using the default, which will use a case sensitive comparison.