Mohanapriya R Mohanapriya R
Updated date Feb 01, 2024
In this article, we will learn the decoding URLs in C#. Follow along with clear examples and a step-by-step program to easily convert URL-encoded strings back to their original form.

Introduction:

URL decoding is a fundamental aspect of web development, allowing developers to convert encoded strings back to their original form. In C#, the process of converting URL-decoded strings to normal strings is straightforward, and this article will guide you through the steps with a practical program and examples.

Understanding URL Decoding:

URL decoding is essential when dealing with data received from URLs. Often, characters in a URL are encoded to ensure safe transmission. For instance, spaces are encoded as '%20', and special characters are represented by their hexadecimal ASCII values. To retrieve the original string, we need to decode these encoded characters.

using System;
using System.Web;

class Program
{
    static void Main()
    {
        // URL-encoded string
        string encodedString = "Hello%20World%21";

        // URL decoding using HttpUtility.UrlDecode
        string decodedString = HttpUtility.UrlDecode(encodedString);

        // Displaying the results
        Console.WriteLine("Encoded String: " + encodedString);
        Console.WriteLine("Decoded String: " + decodedString);
    }
}

In this program, we use the HttpUtility.UrlDecode method from the System.Web namespace to decode the URL-encoded string. The encodedString variable holds the URL-encoded string, and the decodedString variable stores the result after decoding.

Output:

Encoded String: Hello%20World%21
Decoded String: Hello World!

The output demonstrates the successful conversion of the URL-encoded string "Hello%20World%21" back to its original form, "Hello World!".

Conclusion:

In conclusion, decoding URL-encoded strings in C# is a simple yet crucial skill for web developers. The provided program, using the HttpUtility.UrlDecode method, illustrates the ease with which you can convert encoded strings back to their original form. 

Comments (0)

There are no comments. Be the first to comment!!!