GetValues
returns a string[]
if the key exists. An array is zero based, so you get the first element by using array[0]
, you are using GetValues(1)
in your code, i assume that you wanted the first.
You could also use the Enumerable.First
extension method:
Request.QueryString.GetValues("page").First();
Since GetValues
returns not an empty array but null
if the key was not present you need to check that explicitely (FirstOrDefault
doesn't work):
public static String QueryString(string queryStringKey)
{
if (HttpContext.Current != null && HttpContext.Current.Request != null)
{
string[] values = HttpContext.Current.Request.QueryString.GetValues("queryStringKey");
if (values != null) return values.First();
}
return "NAQ";
}