Replace item in querystring

前端 未结 11 1550
甜味超标
甜味超标 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:50

    No, the framework doesn't have any existing QueryStringBuilder class, but usually the querystring information in a HTTP request is available as an iterable and searchable NameValueCollection via the Request.Querystring property.

    Since you are starting off with a Uri object, however, you will need to obtain the querystring portion using the Query property of the Uri object. This will yield a string of the form:

    Uri myURI = new Uri("http://www.mywebsite.com/page.aspx?Val1=A&Val2=B&Val3=C");
    string querystring = myURI.Query;
    
    // Outputs: "?Val1=A&Val2=B&Val3=C". Note the ? prefix!
    Console.WriteLine(querystring);
    

    You can then split this string on the ampersand character to differentiate it into different querystring parameters-value pairs. Then again split each parameter on the "=" character to differentiate it into a key and value.

    Since your final goal is to search for a particular querystring key and if necessary create it, you should try to (re)create a collection (preferably, a generic one) that allows you easily search in the collection, similar to the facility provided by the NameValueCollection class.

提交回复
热议问题