编辑并查找querystring参数值

时间:2016-12-11 09:45:36

标签: c#

首先,我想说我对编码很新,虽然几年前我已经学会了一些基础知识。

现在我正在尝试制作一个搜索用户输入文本的程序。并替换其中的一些部分,然后返回新文本。

像这样:

  

stormcloud-146919.appspot.com/purchase/listing/?global=ahNzfnN0b3JtY2xvdWQtMTQ2OTE5ckULEgNVSUQiEDEyNTA0NjQ5MDgzMjUyNDIMCxIGUGxheWVyGICAgICAgIAKDAsSDEl0ZW1JbnN0YW5jZRiAgICAgOSRCgw&coin= 3 &安培; xpPlayer = 10 &安培; sidekick1 = ahNzfnN0b3JtY2xvdWQtMTQ2OTE5ckULEgNVSUQiEDEyNTA0NjQ5MDgzMjUyNDIMCxIGUGxheWVyGICAgICAgIAKDAsSDEl0ZW1JbnN0YW5jZRiAgICAkMmPCgw&安培; xpSidekick1 =的 10 &安培; sidekick2 = ahNzfnN0b3JtY2xvdWQtMTQ2OTE5ckULEgNVSUQiEDEyNTA0NjQ5MDgzMjUyNDIMCxIGUGxheWVyGICAgICAgIAKDAsSDEl0ZW1JbnN0YW5jZRiAgICA0OqZCgw&安培; xpSidekick2 = 10 &安培; K = ahNzfnN0b3JtY2xvdWQtMTQ2OTE5ciwLEgNVSUQiEDEyNTA0NjQ5MDgzMjUyNDIMCxIGUGxheWVyGICAgICAgIAKDA&安培; L =&ahNzfnN0b3JtY2xvdWQtMTQ2OTE5chQLEgdMaXN0aW5nGICAgID00JwKDA放大器;

搜索粗体部分并替换为我的自定义文本或数字。

2 个答案:

答案 0 :(得分:3)

我希望您希望更新一些查询字符串参数值。 您可以使用HttpUtility.ParseQueryString()。 找到下面的示例。

var url = "stormcloud-146919.appspot.com/purchase/listing/?global=ahNzfnN0b3JtY2xvdWQtMTQ2OTE5ckULEgNVSUQiEDEyNTA0NjQ5MDgzMjUyNDIMCxIGUGxheWVyGICAgICAgIAKDAsSDEl0ZW1JbnN0YW5jZRiAgICAgOSRCgw&coin=3&xpPlayer=10&sidekick1=ahNzfnN0b3JtY2xvdWQtMTQ2OTE5ckULEgNVSUQiEDEyNTA0NjQ5MDgzMjUyNDIMCxIGUGxheWVyGICAgICAgIAKDAsSDEl0ZW1JbnN0YW5jZRiAgICAkMmPCgw&xpSidekick1=10&sidekick2=ahNzfnN0b3JtY2xvdWQtMTQ2OTE5ckULEgNVSUQiEDEyNTA0NjQ5MDgzMjUyNDIMCxIGUGxheWVyGICAgICAgIAKDAsSDEl0ZW1JbnN0YW5jZRiAgICA0OqZCgw&xpSidekick2=10&k=ahNzfnN0b3JtY2xvdWQtMTQ2OTE5ciwLEgNVSUQiEDEyNTA0NjQ5MDgzMjUyNDIMCxIGUGxheWVyGICAgICAgIAKDA&l=ahNzfnN0b3JtY2xvdWQtMTQ2OTE5chQLEgdMaXN0aW5nGICAgID00JwKDA&";
Console.WriteLine(url);
var uriBuilder = new UriBuilder(url);
var query = HttpUtility.ParseQueryString(uriBuilder.Query);
query["coin"] = "4";
query["xpPlayer"] = "4";
query["xpSidekick1"] = "4";
query["xpSidekick2"] = "4";

uriBuilder.Query = query.ToString();
Console.WriteLine(uriBuilder.ToString());

看看它是否符合您的要求。 记得导入System.Web命名空间。

using System.Web;

有关代码的更多详细信息,请点击此处: http://codeskaters.blogspot.ae/2016/12/c-update-querystring-values-in-url.html

答案 1 :(得分:0)

你可以使用         string [] split = input.Split(new Char [] {'&'}); 并为每个分裂.Split(新Char [] {'='}); 替换所需的值并将所有部分连接到新字符串。