Created
October 1, 2013 06:22
-
-
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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