System.UriTemplate.Match C# (CSharp) Method

Match() public method

public Match ( Uri baseAddress, Uri candidate ) : System.UriTemplateMatch
baseAddress Uri
candidate Uri
return System.UriTemplateMatch
		public UriTemplateMatch Match (Uri baseAddress, Uri candidate)
		{
			CheckBaseAddress (baseAddress);
			if (candidate == null)
				throw new ArgumentNullException ("candidate");

			var us = baseAddress.LocalPath;
			if (us [us.Length - 1] != '/')
				baseAddress = new Uri (baseAddress.GetComponents (UriComponents.SchemeAndServer | UriComponents.Path, UriFormat.Unescaped) + '/' + baseAddress.Query, baseAddress.IsAbsoluteUri ? UriKind.Absolute : UriKind.RelativeOrAbsolute);
			if (IgnoreTrailingSlash) {
				us = candidate.LocalPath;
				if (us.Length > 0 && us [us.Length - 1] != '/')
					candidate = new Uri(candidate.GetComponents (UriComponents.SchemeAndServer | UriComponents.Path, UriFormat.Unescaped) + '/' + candidate.Query, candidate.IsAbsoluteUri ? UriKind.Absolute : UriKind.RelativeOrAbsolute);
			}

			if (Uri.Compare (baseAddress, candidate, UriComponents.StrongAuthority, UriFormat.SafeUnescaped, StringComparison.Ordinal) != 0)
				return null;

			int i = 0, c = 0;
			UriTemplateMatch m = new UriTemplateMatch ();
			m.BaseUri = baseAddress;
			m.Template = this;
			m.RequestUri = candidate;
			var vc = m.BoundVariables;

			string cp = Uri.UnescapeDataString (baseAddress.MakeRelativeUri (candidate).ToString ());
			if (IgnoreTrailingSlash && cp [cp.Length - 1] == '/')
				cp = cp.Substring (0, cp.Length - 1);

			int tEndCp = cp.IndexOf ('?');
			if (tEndCp >= 0)
				cp = cp.Substring (0, tEndCp);

			if (template.Length > 0 && template [0] == '/')
				i++;
			if (cp.Length > 0 && cp [0] == '/')
				c++;

			foreach (string name in path) {
				int n = StringIndexOf (template, '{' + name + '}', i);
				if (String.CompareOrdinal (cp, c, template, i, n - i) != 0)
					return null; // doesn't match before current template part.
				c += n - i;
				i = n + 2 + name.Length;
				int ce = cp.IndexOf ('/', c);
				if (ce < 0)
					ce = cp.Length;
				string value = cp.Substring (c, ce - c);
				if (value.Length == 0)
					return null; // empty => mismatch
				vc [name] = value;
				m.RelativePathSegments.Add (value);
				c += value.Length;
			}
			int tEnd = template.IndexOf ('?');
			if (tEnd < 0)
				tEnd = template.Length;
			bool wild = (template [tEnd - 1] == '*');
			if (wild)
				tEnd--;
			if (!wild && (cp.Length - c) != (tEnd - i) ||
			    String.CompareOrdinal (cp, c, template, i, tEnd - i) != 0)
				return null; // suffix doesn't match
			if (wild) {
				c += tEnd - i;
				foreach (var pe in cp.Substring (c).Split (slashSep, StringSplitOptions.RemoveEmptyEntries))
					m.WildcardPathSegments.Add (pe);
			}
			if (candidate.Query.Length == 0)
				return m;


			string [] parameters = Uri.UnescapeDataString (candidate.Query.Substring (1)).Split ('&'); // chop first '?'
			foreach (string parameter in parameters) {
				string [] pair = parameter.Split ('=');
				m.QueryParameters.Add (pair [0], pair [1]);
				if (!query_params.ContainsKey (pair [0]))
					continue;
				string templateName = query_params [pair [0]];
				vc.Add (templateName, pair [1]);
			}

			return m;
		}

Usage Example

        public void TestCompoundFragmentExpansionAssociativeMapVariable()
        {
            string template = "{#keys*}";
            UriTemplate uriTemplate = new UriTemplate(template);
            Uri uri = uriTemplate.BindByName(variables);
            string[] allowed =
                {
                    "#comma=,,dot=.,semi=;",
                    "#comma=,,semi=;,dot=.",
                    "#dot=.,comma=,,semi=;",
                    "#dot=.,semi=;,comma=,",
                    "#semi=;,comma=,,dot=.",
                    "#semi=;,dot=.,comma=,"
                };

            CollectionAssert.Contains(allowed, uri.ToString());

            UriTemplateMatch match = uriTemplate.Match(uri, new[] { "list" }, new[] { "keys" });
            Assert.IsNotNull(match);
            CollectionAssert.AreEqual((ICollection)variables["keys"], (ICollection)match.Bindings["keys"].Value);

            match = uriTemplate.Match(uri, requiredVariables, new[] { "list" }, new[] { "keys" });
            Assert.IsNotNull(match);
            CollectionAssert.AreEqual((ICollection)variables["keys"], (ICollection)match.Bindings["keys"].Value);
        }
All Usage Examples Of System.UriTemplate::Match