/// <summary>
		///		Build a URI from the template.
		/// </summary>
		/// <param name="baseUri">
		///		The base URI, or <c>null</c> to generate a relative URI.
		/// </param>
		/// <param name="templateParameters">
		///		A dictionary containing the template parameters.
		/// </param>
		/// <returns>
		///		The generated URI.
		/// </returns>
		public Uri Populate(Uri baseUri, IDictionary<string, string> templateParameters)
		{
			if (baseUri != null && !baseUri.IsAbsoluteUri)
				throw new UriTemplateException("Base URI '{0}' is not an absolute URI.", baseUri);

			if (templateParameters == null)
				throw new ArgumentNullException("templateParameters");

			TemplateEvaluationContext evaluationContext = new TemplateEvaluationContext(templateParameters);
			StringBuilder uriBuilder = new StringBuilder();
			if (baseUri != null)
			{
				uriBuilder.Append(
					baseUri.GetComponents(UriComponents.Scheme | UriComponents.StrongAuthority, UriFormat.UriEscaped)
				);
			}

			if (_uriSegments.Count > 0)
			{
				foreach (UriSegment uriSegment in _uriSegments)
				{
					string segmentValue = uriSegment.GetValue(evaluationContext);
					if (segmentValue == null)
						continue;

					uriBuilder.Append(
						Uri.EscapeUriString(segmentValue)
					);
					if (uriSegment.IsDirectory)
						uriBuilder.Append('/');
				}
			}
			else
				uriBuilder.Append('/');

			if (_querySegments.Count > 0)
			{
				Action<QuerySegment, char> appendSegment = (segment, separator) =>
				{
					string queryParameterValue = segment.GetValue(evaluationContext);
					if (queryParameterValue == null)
						return;

					string queryParameterName = segment.QueryParameterName;

					uriBuilder.Append(separator);
					uriBuilder.AppendFormat(
						"{0}={1}",
						queryParameterName,
						Uri.EscapeDataString(queryParameterValue)
					);
				};

				appendSegment(_querySegments[0], '?');
				for (int segmentIndex = 1; segmentIndex < _querySegments.Count; segmentIndex++)
					appendSegment(_querySegments[segmentIndex], '&');
			}

			return new Uri(uriBuilder.ToString(), UriKind.RelativeOrAbsolute);
		}
Beispiel #2
0
        /// <summary>
        ///		Build a URI from the template.
        /// </summary>
        /// <param name="baseUri">
        ///		The base URI, or <c>null</c> to generate a relative URI.
        /// </param>
        /// <param name="templateParameters">
        ///		A dictionary containing the template parameters.
        /// </param>
        /// <returns>
        ///		The generated URI.
        /// </returns>
        public Uri Populate(Uri baseUri, IDictionary <string, string> templateParameters)
        {
            if (baseUri != null && !baseUri.IsAbsoluteUri)
            {
                throw new UriTemplateException("Base URI '{0}' is not an absolute URI.", baseUri);
            }

            if (templateParameters == null)
            {
                throw new ArgumentNullException(nameof(templateParameters));
            }

            TemplateEvaluationContext evaluationContext = new TemplateEvaluationContext(templateParameters);
            StringBuilder             uriBuilder        = new StringBuilder();

            if (baseUri != null)
            {
                uriBuilder.Append(
                    baseUri.GetComponents(UriComponents.Scheme | UriComponents.StrongAuthority, UriFormat.UriEscaped)
                    );
            }

            if (_uriSegments.Count > 0)
            {
                foreach (UriSegment uriSegment in _uriSegments)
                {
                    string segmentValue = uriSegment.GetValue(evaluationContext);
                    if (segmentValue == null)
                    {
                        continue;
                    }

                    uriBuilder.Append(
                        Uri.EscapeUriString(segmentValue)
                        );
                    if (uriSegment.IsDirectory)
                    {
                        uriBuilder.Append('/');
                    }
                }
            }
            else
            {
                uriBuilder.Append('/');
            }

            bool isFirstParameterWithValue = true;

            foreach (QuerySegment segment in _querySegments)
            {
                string queryParameterValue = segment.GetValue(evaluationContext);
                if (queryParameterValue == null)
                {
                    continue;
                }

                // Different prefix for first parameter that has a value.
                if (isFirstParameterWithValue)
                {
                    uriBuilder.Append('?');

                    isFirstParameterWithValue = false;
                }
                else
                {
                    uriBuilder.Append('&');
                }

                uriBuilder.AppendFormat(
                    "{0}={1}",
                    Uri.EscapeDataString(segment.QueryParameterName),
                    Uri.EscapeDataString(queryParameterValue)
                    );
            }

            return(new Uri(uriBuilder.ToString(), UriKind.RelativeOrAbsolute));
        }