有没有一种简单的方法来在Windows Phone的从URI获取查询字符串参数? [英] Is there a simple way to get query string parameters from a URI in Windows Phone?
问题描述
我目前使用自定义的URI方案共同验证使用OAuth用户。为了做到这一点,我需要从查询字符串获取某些参数的值。
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?
我以前发现的东西像ParseQueryString引用,但这些都包含在Windows Phone上没有可用的库。
I have previously found references to things like ParseQueryString, but these are contained in libraries not available on Windows Phone.
推荐答案
很多搜索我的后降落在一个简单的方法。所以只要查询字符串保持相当简单(因为它们是在OAuth的)这种方法应该工作
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;
}
这篇关于有没有一种简单的方法来在Windows Phone的从URI获取查询字符串参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!