Created
May 27, 2024 23:16
-
-
Save Robbware/250476d639c0cc477a4db93da7ee345c to your computer and use it in GitHub Desktop.
UriExtensions.AddParameterIfNotNull
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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