How to decode URL-encoded and URL query strings in C#

2 Answers

0 votes
using System;
using System.Net;

namespace UrlDecoderApp
{
    class Program
    {
        /// <summary>
        /// Decodes a URL-encoded string or query component.
        /// Automatically handles percent-encoding (%xx) and '+' to space conversion.
        /// </summary>
        /// <param name="encodedStr">The URL-encoded string.</param>
        /// <returns>The fully decoded string.</returns>
        static string DecodeUrlString(string encodedStr)
        {
            try
            {
                // WebUtility.UrlDecode handles both %xx escapes and '+' to space conversion
                return WebUtility.UrlDecode(encodedStr);
            }
            catch (Exception ex)
            {
                Console.WriteLine($"Decoding error: {ex.Message}");
                return encodedStr;
            }
        }

        static void Main(string[] args)
        {
            // Example 1: Standard URL encoding with special characters
            string sample1 = "Hello%20World%21%20C%23%20%26%20Web";
            Console.WriteLine("--- Example 1: Standard URL Encoding ---");
            Console.WriteLine($"Encoded: {sample1}");
            Console.WriteLine($"Decoded: {DecodeUrlString(sample1)}");

            // Example 2: Form query string fragment encoding (spaces represented as '+')
            string sample2 = "Jane+Doe+from+New+York";
            Console.WriteLine("\n--- Example 2: Form Fragment Encoding ---");
            Console.WriteLine($"Encoded: {sample2}");
            Console.WriteLine($"Decoded: {DecodeUrlString(sample2)}");
        }
    }
}


/*
run:

--- Example 1: Standard URL Encoding ---
Encoded: Hello%20World%21%20C%23%20%26%20Web
Decoded: Hello World! C# & Web

--- Example 2: Form Fragment Encoding ---
Encoded: Jane+Doe+from+New+York
Decoded: Jane Doe from New York

*/

 



answered 1 hour ago by avibootz
0 votes
using System;
using System.Collections.Specialized;
using System.Web;

namespace QueryStringParserApp
{
    class Program
    {
        /// <summary>
        /// Parses and decodes a complete URL query string into a NameValueCollection.
        /// </summary>
        /// <param name="queryString">The URL query string (with or without leading '?').</param>
        /// <returns>A NameValueCollection containing decoded keys and values.</returns>
        static NameValueCollection ParseAndDecodeQuery(string queryString)
        {
            // Remove leading '?' if present
            if (!string.IsNullOrEmpty(queryString) && queryString.StartsWith("?")) {
                queryString = queryString.Substring(1);
            }

            // HttpUtility.ParseQueryString automatically decodes keys and values, 
            // and supports multi-value parameters.
            return HttpUtility.ParseQueryString(queryString);
        }

        static void Main(string[] args)
        {
            string fullQuery = "search=csharp+tutorial&category=programming&tag=beginner&tag=dotnet";

            Console.WriteLine("--- Full Query String Parsing ---");
            Console.WriteLine($"Original Query: {fullQuery}");

            NameValueCollection paramsCollection = ParseAndDecodeQuery(fullQuery);

            Console.WriteLine("Decoded Parameters Result:");
            foreach (string key in paramsCollection.AllKeys)
            {
                // GetValues handles multiple parameters with the same key (e.g., 'tag')
                string[] values = paramsCollection.GetValues(key);
                Console.WriteLine($"  {key} -> [{string.Join(", ", values)}]");
            }
        }
    }
}


/*
run:

--- Full Query String Parsing ---
Original Query: search=csharp+tutorial&category=programming&tag=beginner&tag=dotnet
Decoded Parameters Result:
  search -> [csharp tutorial]
  category -> [programming]
  tag -> [beginner, dotnet]

*/

 



answered 1 hour ago by avibootz
...