如何添加或更新给定 URL 的查询字符串参数?
How to add or update query string parameters to given URL?
使用 C#.Net 如何将查询字符串参数添加到 URL 如果不存在或者如果存在则更新当前值?
例如–
我有 URL - http://example.com/Test.aspx?foo=bar&id=100,我想将 foo 值更新为图表,还想 add/append 新参数作为 hello = world 和 testStatus = true 来查询字符串。
所以最终的预期输出是——
http://example.com/Test.aspx?foo=chart&hello=world&testStatus=true&id=100
Response.Redirect("{newUrl}?param1=value¶m2=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))));
试试这个:
if (Request.QueryString["ParamName"] == null)
//redirect with param in then URL
else
使用发布的答案 here
更新查询字符串的方法如下:Update Query Strings。您可以简单地检查值、更新然后重定向。
我编写了以下函数来完成所需的功能:
/// <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;
}
函数使用-
假设 url 是 - http://example.com/Test.aspx?foo=bar&id=100
并且您想将 foo 值更改为 chart 并且您还想添加新的查询字符串 hello = world and 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
希望这对您有所帮助。
使用 C#.Net 如何将查询字符串参数添加到 URL 如果不存在或者如果存在则更新当前值?
例如–
我有 URL - http://example.com/Test.aspx?foo=bar&id=100,我想将 foo 值更新为图表,还想 add/append 新参数作为 hello = world 和 testStatus = true 来查询字符串。
所以最终的预期输出是——
http://example.com/Test.aspx?foo=chart&hello=world&testStatus=true&id=100
Response.Redirect("{newUrl}?param1=value¶m2=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))));
试试这个:
if (Request.QueryString["ParamName"] == null)
//redirect with param in then URL
else
使用发布的答案 here
更新查询字符串的方法如下:Update Query Strings。您可以简单地检查值、更新然后重定向。
我编写了以下函数来完成所需的功能:
/// <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;
}
函数使用-
假设 url 是 - http://example.com/Test.aspx?foo=bar&id=100
并且您想将 foo 值更改为 chart 并且您还想添加新的查询字符串 hello = world and 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
希望这对您有所帮助。