Skip to content

Commit

Permalink
Added "TryParse" methods to the "HttpQueryString" class
Browse files Browse the repository at this point in the history
  • Loading branch information
abjerner committed Mar 24, 2022
1 parent c9fc87f commit 924dbdb
Showing 1 changed file with 39 additions and 0 deletions.
39 changes: 39 additions & 0 deletions src/Skybrud.Essentials.Http/Collections/HttpQueryString.cs
Original file line number Diff line number Diff line change
Expand Up @@ -342,6 +342,45 @@ public static HttpQueryString ParseQueryString(string str, bool urlencoded) {

}

/// <summary>
/// Converts the specified string representation of a query string to its <see cref="HttpQueryString"/>
/// equivalent and returns a value that indicates whether the conversion succeeded.
/// </summary>
/// <param name="str">A string containing a query string to convert.</param>
/// <param name="result">When this method returns, contains the <see cref="HttpQueryString"/> value equivalent
/// to query string contained in <paramref name="str"/>, if the conversion succeeded, or <c>null</c> if the
/// conversion failed. This parameter is passed uninitialized.</param>
/// <returns><c>true</c> if the <paramref name="str"/> parameter was converted successfully; otherwise, <c>false</c>.</returns>
public bool TryParse(string str, out HttpQueryString result) {
try {
result = ParseQueryString(str);
return true;
} catch {
result = null;
return false;
}
}

/// <summary>
/// Converts the specified string representation of a query string to its <see cref="HttpQueryString"/>
/// equivalent and returns a value that indicates whether the conversion succeeded.
/// </summary>
/// <param name="str">A string containing a query string to convert.</param>
/// <param name="result">When this method returns, contains the <see cref="HttpQueryString"/> value equivalent
/// to query string contained in <paramref name="str"/>, if the conversion succeeded, or <c>null</c> if the
/// conversion failed. This parameter is passed uninitialized.</param>
/// <param name="urlencoded">Whether the query string is URL encoded</param>
/// <returns><c>true</c> if the <paramref name="str"/> parameter was converted successfully; otherwise, <c>false</c>.</returns>
public bool TryParse(string str, bool urlencoded, out HttpQueryString result) {
try {
result = ParseQueryString(str, urlencoded);
return true;
} catch {
result = null;
return false;
}
}

#endregion

#region Operator overloading
Expand Down

0 comments on commit 924dbdb

Please sign in to comment.