Replace item in querystring

前端 未结 11 1484
甜味超标
甜味超标 2020-12-11 01:23

I have a URL that also might have a query string part, the query string might be empty or have multiple items.

I want to replace one of the items in the query string

11条回答
  •  时光取名叫无心
    2020-12-11 01:37

    Lets have this url: https://localhost/video?param1=value1

    At first update specific query string param to new value:

    var uri = new Uri("https://localhost/video?param1=value1");
    var qs = HttpUtility.ParseQueryString(uri.Query);
    qs.Set("param1", "newValue2");
    

    Next create UriBuilder and update Query property to produce new uri with changed param value.

    var uriBuilder = new UriBuilder(uri);
    uriBuilder.Query = qs.ToString();
    var newUri = uriBuilder.Uri;
    

    Now you have in newUri this value: https://localhost/video?param1=newValue2

提交回复
热议问题