The query string in the c# built
In interaction with web resources, it is common demand to build a query string containing the required parameters. Although it seems simple, it will encounter some complex problems, such as additional separators, coding parameters, and ensuring parameters.
In order to simplify this process, using the existing practical program class is an effective method. However, there is no result of thorough search for Microsoft Document Network (MSDN). Therefore, the following provides an alternative solution:
By using , you can get a
instance that can be modified. This set is similar to , allowing adding value. Subsequently, the collection call will generate a query string containing the URL coding value.
HttpUtility.ParseQueryString(string.Empty)
HttpValueCollection
Although is internal and cannot be constructed directly, it can access its functions through the instance obtained. This allows the query string of the operation and retrieval of the URL encoding. NameValueCollection
ToString()
For the .NET CORE application,
<code class="language-csharp">NameValueCollection queryString = System.Web.HttpUtility.ParseQueryString(string.Empty); queryString.Add("key1", "value1"); queryString.Add("key2", "value2"); return queryString.ToString(); // "key1=value1&key2=value2"</code>
The above is the detailed content of How to Efficiently Construct Query Strings in C#?. For more information, please follow other related articles on the PHP Chinese website!