How to update querystring in C#?

后端 未结 11 1195
野的像风
野的像风 2020-12-02 12:06

Somewhere in the url there is a &sortBy=6 . How do I update this to &sortBy=4 or &sortBy=2 on a button click? Do I need to write custom string functions to creat

11条回答
  •  北海茫月
    2020-12-02 12:46

    To modify an existing QueryString value use this approach:

    var nameValues = HttpUtility.ParseQueryString(Request.QueryString.ToString());
    nameValues.Set("sortBy", "4");
    string url = Request.Url.AbsolutePath;
    Response.Redirect(url + "?" + nameValues); // ToString() is called implicitly
    

    I go into more detail in another response.

提交回复
热议问题