Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save chrishasz/6774535 to your computer and use it in GitHub Desktop.

Select an option

Save chrishasz/6774535 to your computer and use it in GitHub Desktop.
Generic method for accessing collections, with a default value override for when Type.DefaultValue returns unsafe data. This specific implementation manages query strings.
public static T GetQueryString<T>(string key, T defaultValue) where T : IConvertible
{
T result = defaultValue;
if (!String.IsNullOrEmpty(HttpContext.Current.Request.QueryString[key]))
{
string value = HttpContext.Current.Request.QueryString[key];
try
{
result = (T)Convert.ChangeType(value, typeof(T));
}
catch
{
result = defaultValue;
}
}
return result;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment