Append values to query string

后端 未结 8 1438
借酒劲吻你
借酒劲吻你 2020-11-28 20:32

I have set of URL\'s similar to the ones below in a list

  • http://somesite.com/backup/lol.php?id=1&server=4&location=us
  • http://somesite.com/news
相关标签:
8条回答
  • 2020-11-28 20:46

    The end to all URL query string editing woes

    After lots of toil and fiddling with the Uri class, and other solutions, here're my string extension methods to solve my problems.

    using System;
    using System.Collections.Specialized;
    using System.Linq;
    using System.Web;
    
    public static class StringExtensions
    {
        public static string AddToQueryString(this string url, params object[] keysAndValues)
        {
            return UpdateQueryString(url, q =>
            {
                for (var i = 0; i < keysAndValues.Length; i += 2)
                {
                    q.Set(keysAndValues[i].ToString(), keysAndValues[i + 1].ToString());
                }
            });
        }
    
        public static string RemoveFromQueryString(this string url, params string[] keys)
        {
            return UpdateQueryString(url, q =>
            {
                foreach (var key in keys)
                {
                    q.Remove(key);
                }
            });
        }
    
        public static string UpdateQueryString(string url, Action<NameValueCollection> func)
        {
            var urlWithoutQueryString = url.Contains('?') ? url.Substring(0, url.IndexOf('?')) : url;
            var queryString = url.Contains('?') ? url.Substring(url.IndexOf('?')) : null;
            var query = HttpUtility.ParseQueryString(queryString ?? string.Empty);
    
            func(query);
    
            return urlWithoutQueryString + (query.Count > 0 ? "?" : string.Empty) + query;
        }
    }
    
    0 讨论(0)
  • 2020-11-28 20:47

    Note you can add the Microsoft.AspNetCore.WebUtilities nuget package from Microsoft and then use this to append values to query string:

    QueryHelpers.AddQueryString(longurl, "action", "login1")
    QueryHelpers.AddQueryString(longurl, new Dictionary<string, string> { { "action", "login1" }, { "attempts", "11" } });
    
    0 讨论(0)
  • 2020-11-28 20:47

    The following solution works for ASP.NET 5 (vNext) and it uses QueryHelpers class to build a URI with parameters.

        public Uri GetUri()
        {
            var location = _config.Get("http://iberia.com");
            Dictionary<string, string> values = GetDictionaryParameters();
    
            var uri = Microsoft.AspNetCore.WebUtilities.QueryHelpers.AddQueryString(location, values);
            return new Uri(uri);
        }
    
        private Dictionary<string,string> GetDictionaryParameters()
        {
            Dictionary<string, string> values = new Dictionary<string, string>
            {
                { "param1", "value1" },
                { "param2", "value2"},
                { "param3", "value3"}
            };
            return values;
        }
    

    The result URI would have http://iberia.com?param1=value1&param2=value2&param3=value3

    0 讨论(0)
  • 2020-11-28 20:48

    My approach is very simple, for beginners :

    // --> Prototype : https://ctrader.guru/?id=1#reload
    
        public static string AddGetParamToUrl(string url, string pname, string pvalue)
        { 
    
            pvalue = Uri.EscapeDataString(pvalue);
    
            if (url.IndexOf("?") > -1)
            {
    
                url = url.Replace("?", string.Format("?{0}={1}&", pname, pvalue));
    
            }
            else if (url.IndexOf("#") > -1)
            {
    
                url = url.Replace("#", string.Format("?{0}={1}#", pname, pvalue));
    
            }
            else
            {
    
                url = string.Format("{0}?{1}={2}", url, pname, pvalue);
    
            }
    
            return url;
    
        }
    
    0 讨论(0)
  • 2020-11-28 20:52

    The provided answers have issues with relative Url's, such as "/some/path/" This is a limitation of the Uri and UriBuilder class, which is rather hard to understand, since I don't see any reason why relative urls would be problematic when it comes to query manipulation.

    Here is a workaround that works for both absolute and relative paths, written and tested in .NET 4:

    (small note: this should also work in .NET 4.5, you will only have to change propInfo.GetValue(values, null) to propInfo.GetValue(values))

      public static class UriExtensions{
        /// <summary>
        ///     Adds query string value to an existing url, both absolute and relative URI's are supported.
        /// </summary>
        /// <example>
        /// <code>
        ///     // returns "www.domain.com/test?param1=val1&amp;param2=val2&amp;param3=val3"
        ///     new Uri("www.domain.com/test?param1=val1").ExtendQuery(new Dictionary&lt;string, string&gt; { { "param2", "val2" }, { "param3", "val3" } }); 
        /// 
        ///     // returns "/test?param1=val1&amp;param2=val2&amp;param3=val3"
        ///     new Uri("/test?param1=val1").ExtendQuery(new Dictionary&lt;string, string&gt; { { "param2", "val2" }, { "param3", "val3" } }); 
        /// </code>
        /// </example>
        /// <param name="uri"></param>
        /// <param name="values"></param>
        /// <returns></returns>
        public static Uri ExtendQuery(this Uri uri, IDictionary<string, string> values) {
          var baseUrl = uri.ToString();
          var queryString = string.Empty;
          if (baseUrl.Contains("?")) {
            var urlSplit = baseUrl.Split('?');
            baseUrl = urlSplit[0];
            queryString = urlSplit.Length > 1 ? urlSplit[1] : string.Empty;
          }
    
          NameValueCollection queryCollection = HttpUtility.ParseQueryString(queryString);
          foreach (var kvp in values ?? new Dictionary<string, string>()) {
            queryCollection[kvp.Key] = kvp.Value;
          }
          var uriKind = uri.IsAbsoluteUri ? UriKind.Absolute : UriKind.Relative;
          return queryCollection.Count == 0 
            ? new Uri(baseUrl, uriKind) 
            : new Uri(string.Format("{0}?{1}", baseUrl, queryCollection), uriKind);
        }
    
        /// <summary>
        ///     Adds query string value to an existing url, both absolute and relative URI's are supported.
        /// </summary>
        /// <example>
        /// <code>
        ///     // returns "www.domain.com/test?param1=val1&amp;param2=val2&amp;param3=val3"
        ///     new Uri("www.domain.com/test?param1=val1").ExtendQuery(new { param2 = "val2", param3 = "val3" }); 
        /// 
        ///     // returns "/test?param1=val1&amp;param2=val2&amp;param3=val3"
        ///     new Uri("/test?param1=val1").ExtendQuery(new { param2 = "val2", param3 = "val3" }); 
        /// </code>
        /// </example>
        /// <param name="uri"></param>
        /// <param name="values"></param>
        /// <returns></returns>
        public static Uri ExtendQuery(this Uri uri, object values) {
          return ExtendQuery(uri, values.GetType().GetProperties().ToDictionary
          (
              propInfo => propInfo.Name,
              propInfo => { var value = propInfo.GetValue(values, null); return value != null ? value.ToString() : null; }
          ));
        }
      }
    

    And here is a suite of unit tests to test the behavior:

      [TestFixture]
      public class UriExtensionsTests {
        [Test]
        public void Add_to_query_string_dictionary_when_url_contains_no_query_string_and_values_is_empty_should_return_url_without_changing_it() {
          Uri url = new Uri("http://www.domain.com/test");
          var values = new Dictionary<string, string>();
          var result = url.ExtendQuery(values);
          Assert.That(result, Is.EqualTo(new Uri("http://www.domain.com/test")));
        }
    
        [Test]
        public void Add_to_query_string_dictionary_when_url_contains_hash_and_query_string_values_are_empty_should_return_url_without_changing_it() {
          Uri url = new Uri("http://www.domain.com/test#div");
          var values = new Dictionary<string, string>();
          var result = url.ExtendQuery(values);
          Assert.That(result, Is.EqualTo(new Uri("http://www.domain.com/test#div")));
        }
    
        [Test]
        public void Add_to_query_string_dictionary_when_url_contains_no_query_string_should_add_values() {
          Uri url = new Uri("http://www.domain.com/test");
          var values = new Dictionary<string, string> { { "param1", "val1" }, { "param2", "val2" } };
          var result = url.ExtendQuery(values);
          Assert.That(result, Is.EqualTo(new Uri("http://www.domain.com/test?param1=val1&param2=val2")));
        }
    
        [Test]
        public void Add_to_query_string_dictionary_when_url_contains_hash_and_no_query_string_should_add_values() {
          Uri url = new Uri("http://www.domain.com/test#div");
          var values = new Dictionary<string, string> { { "param1", "val1" }, { "param2", "val2" } };
          var result = url.ExtendQuery(values);
          Assert.That(result, Is.EqualTo(new Uri("http://www.domain.com/test#div?param1=val1&param2=val2")));
        }
    
        [Test]
        public void Add_to_query_string_dictionary_when_url_contains_query_string_should_add_values_and_keep_original_query_string() {
          Uri url = new Uri("http://www.domain.com/test?param1=val1");
          var values = new Dictionary<string, string> { { "param2", "val2" } };
          var result = url.ExtendQuery(values);
          Assert.That(result, Is.EqualTo(new Uri("http://www.domain.com/test?param1=val1&param2=val2")));
        }
    
        [Test]
        public void Add_to_query_string_dictionary_when_url_is_relative_contains_no_query_string_should_add_values() {
          Uri url = new Uri("/test", UriKind.Relative);
          var values = new Dictionary<string, string> { { "param1", "val1" }, { "param2", "val2" } };
          var result = url.ExtendQuery(values);
          Assert.That(result, Is.EqualTo(new Uri("/test?param1=val1&param2=val2", UriKind.Relative)));
        }
    
        [Test]
        public void Add_to_query_string_dictionary_when_url_is_relative_and_contains_query_string_should_add_values_and_keep_original_query_string() {
          Uri url = new Uri("/test?param1=val1", UriKind.Relative);
          var values = new Dictionary<string, string> { { "param2", "val2" } };
          var result = url.ExtendQuery(values);
          Assert.That(result, Is.EqualTo(new Uri("/test?param1=val1&param2=val2", UriKind.Relative)));
        }
    
        [Test]
        public void Add_to_query_string_dictionary_when_url_is_relative_and_contains_query_string_with_existing_value_should_add_new_values_and_update_existing_ones() {
          Uri url = new Uri("/test?param1=val1", UriKind.Relative);
          var values = new Dictionary<string, string> { { "param1", "new-value" }, { "param2", "val2" } };
          var result = url.ExtendQuery(values);
          Assert.That(result, Is.EqualTo(new Uri("/test?param1=new-value&param2=val2", UriKind.Relative)));
        }
    
        [Test]
        public void Add_to_query_string_object_when_url_contains_no_query_string_should_add_values() {
          Uri url = new Uri("http://www.domain.com/test");
          var values = new { param1 = "val1", param2 = "val2" };
          var result = url.ExtendQuery(values);
          Assert.That(result, Is.EqualTo(new Uri("http://www.domain.com/test?param1=val1&param2=val2")));
        }
    
    
        [Test]
        public void Add_to_query_string_object_when_url_contains_query_string_should_add_values_and_keep_original_query_string() {
          Uri url = new Uri("http://www.domain.com/test?param1=val1");
          var values = new { param2 = "val2" };
          var result = url.ExtendQuery(values);
          Assert.That(result, Is.EqualTo(new Uri("http://www.domain.com/test?param1=val1&param2=val2")));
        }
    
        [Test]
        public void Add_to_query_string_object_when_url_is_relative_contains_no_query_string_should_add_values() {
          Uri url = new Uri("/test", UriKind.Relative);
          var values = new { param1 = "val1", param2 = "val2" };
          var result = url.ExtendQuery(values);
          Assert.That(result, Is.EqualTo(new Uri("/test?param1=val1&param2=val2", UriKind.Relative)));
        }
    
        [Test]
        public void Add_to_query_string_object_when_url_is_relative_and_contains_query_string_should_add_values_and_keep_original_query_string() {
          Uri url = new Uri("/test?param1=val1", UriKind.Relative);
          var values = new { param2 = "val2" };
          var result = url.ExtendQuery(values);
          Assert.That(result, Is.EqualTo(new Uri("/test?param1=val1&param2=val2", UriKind.Relative)));
        }
    
        [Test]
        public void Add_to_query_string_object_when_url_is_relative_and_contains_query_string_with_existing_value_should_add_new_values_and_update_existing_ones() {
          Uri url = new Uri("/test?param1=val1", UriKind.Relative);
          var values = new { param1 = "new-value", param2 = "val2" };
          var result = url.ExtendQuery(values);
          Assert.That(result, Is.EqualTo(new Uri("/test?param1=new-value&param2=val2", UriKind.Relative)));
        }
      }
    
    0 讨论(0)
  • 2020-11-28 20:55

    I like Bjorn's answer, however the solution he's provided is misleading, as the method updates an existing parameter, rather than adding it if it doesn't exist.. To make it a bit safer, I've adapted it below.

    public static class UriExtensions
    {
        /// <summary>
        /// Adds or Updates 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 AddOrUpdateParameter(this Uri url, string paramName, string paramValue)
        {
            var uriBuilder = new UriBuilder(url);
            var query = HttpUtility.ParseQueryString(uriBuilder.Query);
    
            if (query.AllKeys.Contains(paramName))
            {
                query[paramName] = paramValue;
            }
            else
            {
                query.Add(paramName, paramValue);
            }
            uriBuilder.Query = query.ToString();
    
            return uriBuilder.Uri;
        }
    }
    
    0 讨论(0)
提交回复
热议问题