I'm currently working with a Custom URI Scheme to validate users using OAuth. In order to do this, I need to get the values of certain parameters from the query string.
Is there a simple way to get this information? Or is my only option to using REGEX or other string manipulation?
I have previously found references to things like ParseQueryString, but these are contained in libraries not available on Windows Phone.
The Query property contains any query information included in the URI. Query information is separated from the path information by a question mark (?) and continues to the end of the URI. The query information returned includes the leading question mark.
A query string is a part of a uniform resource locator (URL) that assigns values to specified parameters.
To pass in parameter values, simply append them to the query string at the end of the base URL. In the above example, the view parameter script name is viewParameter1.
After a lot of searching I landed on a simple approach. So long as query strings are kept fairly simple (as they are in OAuth) this method should work.
public static Dictionary<string, string> ParseQueryString( string uri ) {
string substring = uri.Substring( ( ( uri.LastIndexOf('?') == -1 ) ? 0 : uri.LastIndexOf('?') + 1 ) );
string[] pairs = substring.Split( '&' );
Dictionary<string,string> output = new Dictionary<string,string>();
foreach( string piece in pairs ){
string[] pair = piece.Split( '=' );
output.Add( pair[0], pair[1] );
}
return output;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With