如何将查询字符串参数添加或更新到给定的URL?

时间:2015-03-23 07:21:05

标签: c# asp.net c#-4.0 c#-3.0

使用C#.Net,如果不存在,如何将查询字符串参数添加到URL,如果存在,则更新当前值?

例如 -
我有URL - http://example.com/Test.aspx?foo=bar&id=100,我想将foo值更新为图表,并且还希望将新参数添加/追加为hello = world和testStatus = true来查询字符串。

所以最终的预期产量是 - http://example.com/Test.aspx?foo=chart&hello=world&testStatus=true&id=100

4 个答案:

答案 0 :(得分:1)

Response.Redirect("{newUrl}?param1=value&param2=value")

编辑:

只需通过Request.QueryString。

Dictionary params = new Dictionary<string,string>();
foreach (string key in Request.QueryString)
{
    var value = Request.QueryString[key];
    //
    //Do everything you need with params
    //
    params.Add(key, value);
}
Response.Redirect("{newUrl}?" + string.Join("&", params.Select(x=>string.Format("{0}={1}", x.Key, x.Value))));

答案 1 :(得分:1)

我编写了以下用于完成所需功能的函数:

        /// <summary>
        /// Get URL With QueryString Dynamically
        /// </summary>
        /// <param name="url">URI With/Without QueryString</param>
        /// <param name="newQueryStringArr">New QueryString To Append</param>
        /// <returns>Return Url + Existing QueryString + New/Modified QueryString</returns>
        public string BuildQueryStringUrl(string url, string[] newQueryStringArr)
        {
            string plainUrl;
            var queryString = string.Empty;

            var newQueryString = string.Join("&", newQueryStringArr);

            if (url.Contains("?"))
            {
                var index = url.IndexOf('?');
                plainUrl = url.Substring(0, index); //URL With No QueryString
                queryString = url.Substring(index + 1);
            }
            else
            {
                plainUrl = url;
            }

            var nvc = HttpUtility.ParseQueryString(queryString);
            var qscoll = HttpUtility.ParseQueryString(newQueryString);

            var queryData = string.Join("&",
                nvc.AllKeys.Where(key => !qscoll.AllKeys.Any(newKey => newKey.Contains(key))).
                    Select(key => string.Format("{0}={1}",
                        HttpUtility.UrlEncode(key), HttpUtility.UrlEncode(nvc[key]))).ToArray());
            //Fetch Existing QueryString Except New QueryString

            var delimiter = nvc.HasKeys() && !string.IsNullOrEmpty(queryData) ? "&" : string.Empty;
            var queryStringToAppend = "?" + newQueryString + delimiter + queryData;

            return plainUrl + queryStringToAppend;
        }        

功能使用 -

假设给定的网址是 - http://example.com/Test.aspx?foo=bar&id=100
您希望将 foo 值更改为图表,并且还要添加新的查询字符串,例如 hello = world testStatus = true 然后 -

方法调用的输入:

BuildQueryStringUrl("http://example.com/Test.aspx?foo=bar&id=100",
                new[] {"foo=chart", "hello=world", "testStatus=true"});

输出: http://example.com/Test.aspx?foo=chart&hello=world&testStatus=true&id=100

希望这有帮助。

答案 2 :(得分:0)

试试这个:

if (Request.QueryString["ParamName"] == null)
  //redirect with param in then URL
else

使用已发布的答案here

答案 3 :(得分:0)

以下是更新查询字符串的方法:Update Query Strings。您只需检查值,更新然后重定向即可。