From 1aed739edb6a8fef1830fb86748a1334ae7f6b35 Mon Sep 17 00:00:00 2001 From: Chris Ross Date: Fri, 22 Aug 2014 09:04:30 -0700 Subject: [PATCH] WebUtilities: Add more query helpers. --- .../FormHelpers.cs | 5 +++- .../QueryHelpers.cs | 29 ++++++++++++++++++- 2 files changed, 32 insertions(+), 2 deletions(-) diff --git a/src/Microsoft.AspNet.WebUtilities/FormHelpers.cs b/src/Microsoft.AspNet.WebUtilities/FormHelpers.cs index 17f8135465..4c16485eb0 100644 --- a/src/Microsoft.AspNet.WebUtilities/FormHelpers.cs +++ b/src/Microsoft.AspNet.WebUtilities/FormHelpers.cs @@ -1,4 +1,7 @@ -using System; +// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. +// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. + +using System; using Microsoft.AspNet.Http; namespace Microsoft.AspNet.WebUtilities diff --git a/src/Microsoft.AspNet.WebUtilities/QueryHelpers.cs b/src/Microsoft.AspNet.WebUtilities/QueryHelpers.cs index 0b4de851bc..d31a250fdb 100644 --- a/src/Microsoft.AspNet.WebUtilities/QueryHelpers.cs +++ b/src/Microsoft.AspNet.WebUtilities/QueryHelpers.cs @@ -1,4 +1,9 @@ -using System; +// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. +// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. + +using System; +using System.Collections.Generic; +using System.Text; namespace Microsoft.AspNet.WebUtilities { @@ -16,5 +21,27 @@ namespace Microsoft.AspNet.WebUtilities bool hasQuery = uri.IndexOf('?') != -1; return uri + (hasQuery ? "&" : "?") + Uri.EscapeDataString(name) + "=" + Uri.EscapeDataString(value); } + + /// + /// Append the given query keys and values to the uri. + /// + /// The base uri. + /// A collection of name value query pairs to append. + /// The combine result. + public static string AddQueryString([NotNull] string uri, [NotNull] IDictionary queryString) + { + var sb = new StringBuilder(); + sb.Append(uri); + bool hasQuery = uri.IndexOf('?') != -1; + foreach (var parameter in queryString) + { + sb.Append(hasQuery ? '&' : '?'); + sb.Append(Uri.EscapeDataString(parameter.Key)); + sb.Append('='); + sb.Append(Uri.EscapeDataString(parameter.Value)); + hasQuery = true; + } + return sb.ToString(); + } } } \ No newline at end of file