Append values to query string

后端 未结 8 1484
借酒劲吻你
借酒劲吻你 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 21:04

    You could use the HttpUtility.ParseQueryString method and an UriBuilder which provides a nice way to work with query string parameters without worrying about things like parsing, url encoding, ...:

    string longurl = "http://somesite.com/news.php?article=1&lang=en";
    var uriBuilder = new UriBuilder(longurl);
    var query = HttpUtility.ParseQueryString(uriBuilder.Query);
    query["action"] = "login1";
    query["attempts"] = "11";
    uriBuilder.Query = query.ToString();
    longurl = uriBuilder.ToString();
    // "http://somesite.com:80/news.php?article=1&lang=en&action=login1&attempts=11"
    

提交回复
热议问题