Skip to content

Commit

Permalink
Added null checks to the "HttpQueryString.Add" and "HttpQueryString.S…
Browse files Browse the repository at this point in the history
…et" methods
  • Loading branch information
abjerner committed Mar 24, 2022
1 parent 1c88821 commit c9fc87f
Showing 1 changed file with 17 additions and 0 deletions.
17 changes: 17 additions & 0 deletions src/Skybrud.Essentials.Http/Collections/HttpQueryString.cs
Original file line number Diff line number Diff line change
Expand Up @@ -101,8 +101,15 @@ public HttpQueryString(NameValueCollection collection) {
/// <param name="key">The key of the entry.</param>
/// <param name="value">The value of the entry.</param>
public void Add(string key, object value) {

if (string.IsNullOrWhiteSpace(key)) throw new ArgumentNullException(nameof(key));

// Abort if "value" is null
if (value == null) return;

// Convert the value to a culture invariant string and add it to the dictionary
_values.Add(key, string.Format(CultureInfo.InvariantCulture, "{0}", value));

}

/// <summary>
Expand All @@ -111,8 +118,18 @@ public void Add(string key, object value) {
/// <param name="key">The key of the entry.</param>
/// <param name="value">The value of the entry.</param>
public void Set(string key, object value) {

if (string.IsNullOrWhiteSpace(key)) throw new ArgumentNullException(nameof(key));

// Specifying a null value should result in the item being removed
if (value == null) {
_values.Remove(key);
return;
}

// Convert the value to a culture invariant string and set it in the dictionary
_values[key] = string.Format(CultureInfo.InvariantCulture, "{0}", value);

}

/// <summary>
Expand Down

0 comments on commit c9fc87f

Please sign in to comment.