How to split URI in WP7?

Is there a way to access query parameters in a WebBrowser control, or do we need to manually split the string? For instance:

http://www.mysite.com?paramter=12345

I just need to access the value of the parameter. I know that when working with xaml pages we have a QueryString property. Is there something similar for working with web pages?

+3
source share
4 answers

I can’t remember where I got this, possibly SO.

 public static class UriExtensions
    {
        private static readonly Regex QueryStringRegex = new Regex(@"[\?&](?<name>[^&=]+)=(?<value>[^&=]+)");

        public static IEnumerable<KeyValuePair<string, string>> ParseQueryString(this Uri uri)
        {
            if (uri == null)
                throw new ArgumentException("uri");

            var matches = QueryStringRegex.Matches(uri.OriginalString);
            for (var i = 0; i < matches.Count; i++)
            {
                var match = matches[i];
                yield return new KeyValuePair<string, string>(match.Groups["name"].Value, match.Groups["value"].Value);
            }
        }
    }
+5
source

Some important optimizations from other answers:

  • C # call in Uri
  • handling a possible missing value of type & name = in Uri
  • not forgetting Uri.UnescapeDataString
  • returns Dictionaryinstead IEnumerableto easily find the desired parameter
  • , Uri.Query WP7

Regex (, Split):

static readonly Regex QueryStringRegex1 = new Regex(@"^[^#]*\?([^#]*)");
static readonly Regex QueryStringRegex2 = new Regex(@"(?<name>[^&=]+)=(?<value>[^&=]*)");
public static Dictionary<string, string> QueryDictionary(this Uri uri)
{
    return QueryStringRegex1.Match(uri.ToString())// between ? and #
        .Groups
        .Cast<Group>()
        .Select(a => QueryStringRegex2.Matches(a.Value)// between ? and &
            .Cast<Match>()
            .Select(b => b.Groups)
            .ToDictionary(b => Uri.UnescapeDataString(b["name"].Value), b => Uri.UnescapeDataString(b["value"].Value)))
        .ElementAtOrDefault(1)
        ?? new Dictionary<string, string>();
}

Split ( , Regex, ):

static readonly char[] QueryStringSeparator1 = "#".ToCharArray();
static readonly char[] QueryStringSeparator2 = "?".ToCharArray();
static readonly char[] QueryStringSeparator3 = "&".ToCharArray();
static readonly char[] QueryStringSeparator4 = "=".ToCharArray();
public static Dictionary<string, string> QueryDictionary(this Uri uri)
{
    return uri.ToString()
        .Split(QueryStringSeparator1, StringSplitOptions.RemoveEmptyEntries)
        .Select(a => a.Split(QueryStringSeparator2, StringSplitOptions.RemoveEmptyEntries)
            .Select(b => b.Split(QueryStringSeparator3, StringSplitOptions.RemoveEmptyEntries)
                .Select(c => c.Split(QueryStringSeparator4))
                .Where(c => c[0].Length > 0)
                .ToDictionary(c => Uri.UnescapeDataString(c[0]), c => c.Length > 1 ? Uri.UnescapeDataString(c[1]) : ""))
            .ElementAtOrDefault(1))// after ?
        .FirstOrDefault()// before #
        ?? new Dictionary<string, string>();
}

. Windows Phone 7 Uri.Query Uri, "mailto: a@example.com? subject = subject & body = body". Uri.ToString() ( , Uri.OriginalString). "(http | https)://" Windows Phone 8, fooobar.com/questions/1152295/....

Uri.Query Windows Phone 8 Windows Phone 7, :

public static readonly bool IsVersion8 = Environment.OSVersion.Version >= new Version(8, 0);
+2

If anyone else has another way to do this, I was able to do the exact same thing manually:

// The url looked something like this http://www.mysite.com?param1=value&param2=value
var parameterValue = e.Uri.Query.Split('&')
            .Where(s => s.Split('=')[0] == "param2")
            .Select(s => s.Split('=')[1])
            .FirstOrDefault();

I don’t think this path is bad, I’m just wondering if there is a built-in method for this type of parsing, because I know that it exists in ASP.NET, .NET, etc.

+1
source

Inspired by the answer to a similar question ,

public static Dictionary<string, string> ParseQueryString(Uri uri)
{
    var substring = uri.Query;
    if (substring.Length > 0)
         substring.Substring(1);

    var pairs = substring.Split('&'); 
    var output = new Dictionary<string, string>(pairs.Length);

    foreach (string piece in pairs)
    {
        var pair = piece.Split('=');
        output.Add(
            Uri.UnescapeDataString(pair[0]),
            Uri.UnescapeDataString(pair[1]));
    }

    return output;
}
0
source

All Articles