Skip to content

Instantly share code, notes, and snippets.

@Robbware
Created May 27, 2024 23:16
Show Gist options
  • Save Robbware/250476d639c0cc477a4db93da7ee345c to your computer and use it in GitHub Desktop.
Save Robbware/250476d639c0cc477a4db93da7ee345c to your computer and use it in GitHub Desktop.
UriExtensions.AddParameterIfNotNull
public static class UriExtensions
{
/// <summary>
/// Adds the specified parameter to the Query String.
/// </summary>
/// <param name="url"></param>
/// <param name="paramName">Name of the parameter to add.</param>
/// <param name="paramValue">Value for the parameter to add.</param>
/// <returns>Url with added parameter.</returns>
public static Uri AddParameterIfNotNull(this Uri url, string paramName, string paramValue)
{
if (string.IsNullOrEmpty(paramValue))
{
return url;
}
var uriBuilder = new UriBuilder(url);
var query = HttpUtility.ParseQueryString(uriBuilder.Query);
query[paramName] = paramValue;
uriBuilder.Query = query.ToString();
return uriBuilder.Uri;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment