如何更改查询字符串参数的值?
How do I change the value of a query string parameter?
我有一个 URL 的字符串表示形式,如下所示:
http://www.GoodStuff.xxx/services/stu/query?where=1%3D1&text=&objectIds=231699%2C232002%2C231700%2C100646&time=
它是一个 URL 但在我的代码中是一个字符串对象。如何更改 objectIds
的值?是否需要找到字符串objectIds
,然后找到前后的&
,并用想要的值替换其中的内容?或者有更好的方法吗?
这是一个 .NET 4.5 FW 控制台应用程序...
如果 URL 的其余部分是固定的,您可以手动找到 ID,然后使用 string.Format
和 string.Join
将 ID 插入其中:
var urlString = string.Format(
"http://www.GoodStuff.xxx/services/stu/query?where=1%3D1&text=&objectIds={0}&time="
, string.Join("%", ids)
);
这将从您的代码中插入 %
分隔的 ids
列表到 URL 模板中。
如果您要替换已经存在的值,那就有点棘手了。试试这个。
//Base URL. Doesn't need to be hardcoded. As long as it contains "objectIds=" then it will work
static string url = @"http://www.GoodStuff.xxx/services/stu/query?where=1%3D1&text=&objectIds=231699%2C232002%2C231700%2C100646&time=";
static void Main(string[] args)
{
//Get the start index
// +10 because IndexOf gets us to the o but we want the index of the equal sign
int startIndex = url.IndexOf("objectIds=") + 10;
//Figure out how many characters we are skipping over.
//This is nice because then it doesn't matter if the value of objectids is 0 or 99999999
int endIndex = url.Substring(startIndex).IndexOf('&');
//Cache the second half of the URL
String secondHalfOfURL = url.Substring(startIndex + endIndex);
//Our new IDs to stick in
int newObjectIDs = 12345;
//The new URL.
//First, we get the string up to the equal sign of the objectIds value
//Next we put our IDS in.
//Finally we add on the second half of the URL
String NewURL = url.Substring(0, startIndex) + newObjectIDs + secondHalfOfURL;
Console.WriteLine(NewURL);
Console.Read();
}
它不是很漂亮,但它确实起作用了。
我有一个 URL 的字符串表示形式,如下所示:
http://www.GoodStuff.xxx/services/stu/query?where=1%3D1&text=&objectIds=231699%2C232002%2C231700%2C100646&time=
它是一个 URL 但在我的代码中是一个字符串对象。如何更改 objectIds
的值?是否需要找到字符串objectIds
,然后找到前后的&
,并用想要的值替换其中的内容?或者有更好的方法吗?
这是一个 .NET 4.5 FW 控制台应用程序...
如果 URL 的其余部分是固定的,您可以手动找到 ID,然后使用 string.Format
和 string.Join
将 ID 插入其中:
var urlString = string.Format(
"http://www.GoodStuff.xxx/services/stu/query?where=1%3D1&text=&objectIds={0}&time="
, string.Join("%", ids)
);
这将从您的代码中插入 %
分隔的 ids
列表到 URL 模板中。
如果您要替换已经存在的值,那就有点棘手了。试试这个。
//Base URL. Doesn't need to be hardcoded. As long as it contains "objectIds=" then it will work
static string url = @"http://www.GoodStuff.xxx/services/stu/query?where=1%3D1&text=&objectIds=231699%2C232002%2C231700%2C100646&time=";
static void Main(string[] args)
{
//Get the start index
// +10 because IndexOf gets us to the o but we want the index of the equal sign
int startIndex = url.IndexOf("objectIds=") + 10;
//Figure out how many characters we are skipping over.
//This is nice because then it doesn't matter if the value of objectids is 0 or 99999999
int endIndex = url.Substring(startIndex).IndexOf('&');
//Cache the second half of the URL
String secondHalfOfURL = url.Substring(startIndex + endIndex);
//Our new IDs to stick in
int newObjectIDs = 12345;
//The new URL.
//First, we get the string up to the equal sign of the objectIds value
//Next we put our IDS in.
//Finally we add on the second half of the URL
String NewURL = url.Substring(0, startIndex) + newObjectIDs + secondHalfOfURL;
Console.WriteLine(NewURL);
Console.Read();
}
它不是很漂亮,但它确实起作用了。