SoFunction
Updated on 2025-03-07

C# method to analyze URL parameters and get the corresponding list of parameters and values

This article describes the method of C# to analyze URL parameters to obtain the corresponding list of parameters and values. Share it for your reference. The specific analysis is as follows:

This C# function is used to analyze all parameters passed in the url, and output a NameValueCollection list corresponding to the parameter name and parameter value. It can be used to obtain

/// <summary>
/// Analyze parameter information in the url string/// </summary>
/// <param name="url">Entered URL</param>/// <param name="baseUrl">Output basic part of URL</param>/// <param name="nvc">Set of (parameter name, parameter value) obtained after output analysis</param>public static void ParseUrl(string url, out string baseUrl, out NameValueCollection nvc)
{
  if (url == null)
 throw new ArgumentNullException("url");
  nvc = new NameValueCollection();
  baseUrl = "";
  if (url == "")
 return;
  int questionMarkIndex = ('?');
  if (questionMarkIndex == -1)
  {
 baseUrl = url;
 return;
  }
  baseUrl = (0, questionMarkIndex);
  if (questionMarkIndex ==  - 1)
 return;
  string ps = (questionMarkIndex + 1);
  // Start analyzing parameter pairs  Regex re = new Regex(@"(^|&amp;)?(\w+)=([^&amp;]+)(&amp;|$)?",);
  MatchCollection mc = (ps);
  foreach (Match m in mc)
  {
 (("$2").ToLower(), ("$3"));
  }
}

I hope this article will be helpful to everyone's C# programming.