/// <summary> /// Add or update a set of query string parameters using the supplied /// name value dictionary. If a value is null the param is removed. /// </summary> /// <param name="source">Uri to perform the operation on.</param> /// <param name="newNameValues">New name value dictionary.</param> /// <returns>A new Uri instance with the added or modified query string parameters.</returns> /// <exception cref="T:System.ArgumentNullException"><paramref name="source" /> is null.</exception> /// <exception cref="T:System.ArgumentNullException"><paramref name="newNameValues" /> is null.</exception> public static Uri AddOrUpdateQueryParams(this Uri source, IDictionary <string, string> newNameValues) { if (source == null) { throw new ArgumentNullException(nameof(source)); } if (newNameValues == null) { throw new ArgumentNullException(nameof(newNameValues)); } var nameValues = UriQueryConverter.ToNameValueCollection(source.Query); foreach (string key in newNameValues.Keys) { var newValue = newNameValues[key]; if (newValue == null) { nameValues.Remove(key); } else { nameValues.AddOrUpdate(key, newValue); } } return(source.SetQuery(nameValues)); }
/// <summary> /// Add or update a query string parameter. If the value is null the param is removed. /// </summary> /// <param name="source">Uri to perform the operation on.</param> /// <param name="name">Query string parameter name.</param> /// <param name="value">Query string parameter value.</param> /// <returns>A Uri with the added or modified query string parameter.</returns> /// <exception cref="T:System.ArgumentNullException"><paramref name="source" /> is null.</exception> /// <exception cref="T:System.ArgumentException"><paramref name="name" /> was null or empty.</exception> public static Uri AddOrUpdateQueryParam(this Uri source, string name, string value) { if (source == null) { throw new ArgumentNullException(nameof(source)); } if (string.IsNullOrEmpty(name)) { throw new ArgumentException("Parameter name was null or empty.", nameof(name)); } var nameValues = UriQueryConverter.ToNameValueCollection(source.Query); if (value == null) { nameValues.Remove(name); } else { nameValues.AddOrUpdate(name, value); } return(source.SetQuery(nameValues)); }
/// <summary> /// Returns the Uri query string as a NameValueCollection. /// </summary> /// <param name="source">Uri to perform the operation on.</param> /// <returns>NameValueCollection of query string name value pairs.</returns> public static NameValueCollection QueryToNameValueCollection(this Uri source) { if (source == null) { return(new NameValueCollection()); } return(UriQueryConverter.ToNameValueCollection(source.Query)); }
/// <summary> /// Returns the Uri query string as a Dictionary. /// </summary> /// <param name="source">Uri to perform the operation on.</param> /// <returns>Dictionary of query string name value pairs.</returns> public static IDictionary <string, string> QueryToDictionary(this Uri source) { if (source == null) { return(new Dictionary <string, string>()); } return(UriQueryConverter.ToDictionary(source.Query)); }
/// <summary> /// Remove query string parameter. /// </summary> /// <param name="source">Uri to perform the operation on.</param> /// <param name="name">The name of the parameter to remove.</param> /// <returns>Uri with any matching parameters removed.</returns> public static Uri RemoveQueryParam(this Uri source, string name) { if (source == null) { return(null); } if (string.IsNullOrEmpty(name)) { return(source); } var nameValues = UriQueryConverter.ToNameValueCollection(source.Query); nameValues.Remove(name); return(source.SetQuery(nameValues)); }
/// <summary> /// Remove query string parameters. /// </summary> /// <param name="source">Uri to perform the operation on.</param> /// <param name="names">The names of the parameters to remove.</param> /// <returns>Uri with any matching parameters removed.</returns> public static Uri RemoveQueryParams(this Uri source, IEnumerable <string> names) { if (source == null) { return(null); } if (names == null) { return(source); } var nameValues = UriQueryConverter.ToNameValueCollection(source.Query); foreach (var name in names) { nameValues.Remove(name); } return(source.SetQuery(nameValues)); }
/// <summary> /// Returns a new Uri with the query set. /// </summary> /// <param name="source">Uri to perform the operation on.</param> /// <param name="queryNames">Query names to set.</param> /// <returns>New Uri instance with the query set.</returns> /// <exception cref="T:System.ArgumentNullException"><paramref name="source" /> is null.</exception> public static Uri SetQuery(this Uri source, IEnumerable <string> queryNames) { var queryString = UriQueryConverter.ToString(queryNames); return(SetQuery(source, queryString)); }
/// <summary> /// Returns a new Uri with the query set. /// </summary> /// <param name="source">Uri to perform the operation on.</param> /// <param name="queryNameValues">Query name values to set.</param> /// <returns>New Uri instance with the query set.</returns> /// <exception cref="T:System.ArgumentNullException"><paramref name="source" /> is null.</exception> public static Uri SetQuery(this Uri source, NameValueCollection queryNameValues) { var queryString = UriQueryConverter.ToString(queryNameValues); return(SetQuery(source, queryString)); }