SoFunction
Updated on 2025-03-07

How to parse and get Url parameter value in C#

Today I encountered a requirement that I needed to process a parameter passed through the interface. The parameter content is a spliced ​​Url address, and the address will also carry some additional parameters, including but not limited to numbers, strings, and json strings. Examples are as follows:

https://shequ./?url=/?id=15&data={"id":12,"name":"The weather is good today"}

The problem now is that I need to modify the id in the json of the data value in the url parameter to the id value in the url parameter, and then splice it into a complete url to get the data. Simply put, it is to change the url as follows. The focus of the problem now lies in how to obtain the corresponding parameter value based on the url.

//Before processinghttps:///?id=15&data={"id":12,"name":"The weather is good today"}
//After processinghttps:///?id=15&data={"id":15,"name":"The weather is good today"}

Url encoding

Here is a sentence: When requesting url as a parameter, you need to pay attention to the impact of some special characters (such as ? "" /, etc.) on the request, especially when sending Get requests. The URL can be encoded before sending the request.

encodeURIComponent(url) //js
(url) //C#
(url) //Go

Get Url parameters

After careful consideration, the parsing of url to obtain parameters is nothing more than the splitting of keyword characters. The difference is that the way to split characters is to match or traverse characters through regular matching. The following is a method of frame belt, and then summarize some rules and finally implement one method by yourself.

I know there are definitely many friends who use it frequentlyHttpUtilityIn the classUrlEncode/UrlDecodeMethod, there is actually anotherParseQueryStringMethod, used to parse url parameters. This method needs to pass in the url parameter part string and then return aNameValueCollectionObject (all parameters are recorded in key/value).

string url = "https:///?id=15&data={'id':12,'name':'The weather is good today'}";var uri = new Uri(url);

var collection= ();//UTF-8 encoding is used by default, of course, specific encodings can also be passed in for parsing//var collection= (,);

(collection["data"]);//Output result: {'id':12,'name':'The weather is good today'}

There are several points to pay attention to in this method

  1. Yes, beginning, but after being converted to a key-value pair, it is automatically filtered out, and only one character is filtered
  2. Parameter content will be automatically decoded using UTF-8
  3. For the passed array parameter (?ids=1&ids=2), it will pass,
  4. After passing & in parameter characters, only the first string before = will be used as key

Based on the above requirements, you can write a parsing parameter method by yourself.

public static Dictionary<string, string> ParseQueryString(string url)
{
	if ((url))
	{
		throw new ArgumentNullException("url");
	}
	var uri = new Uri(url);
	if (())
	{
		return new Dictionary<string, string>();
	}
 //1. Remove the first leading character?	var dic = (1)
 //2. Divide each parameter by &			.Split(new char[] { '&' }, )
 //3. Divide the parameters key and value by =, and ensure that only the first = character is divided			.Select(param => (new char[] { '=' }, 2, ))
 //4. Grouping by the same parameter key			.GroupBy(part => part[0], part =>  > 1 ? part[1] : )
 //5. Splice the value of the same key to the splice			.ToDictionary(group => , group => (",", group));

	return dic;
}

Summarize

Overall, this is just a small functional point. Often, the framework itself helps us encapsulate the method of analyzing the URL to obtain parameters, but when we implement it ourselves, there may be situations that are not taken into account. Finally, if you have friends who have similar needs, I suggest using it()Right~~~

The above is the detailed content of how to parse and obtain Url parameter values ​​in C#. For more information about parse and obtaining Url parameter values ​​in c#, please pay attention to my other related articles!