Uri.EscapeDataString(String) Method in C#

The Uri.EscapeDataString() method in C# converts a string to its escaped representation by encoding characters that are not safe for use in URI data strings. This method is essential when you need to include user data or special characters in URL components like query parameters.

Syntax

Following is the syntax −

public static string EscapeDataString(string stringToEscape);

Parameters

stringToEscape: A string that contains the data to be escaped. If the string is null, the method returns null.

Return Value

Returns a string that contains the escaped representation of stringToEscape.

How It Works

The method follows RFC 3986 standards and escapes characters by replacing them with percent-encoded sequences. Characters like spaces, colons, slashes, and other special characters are converted to their hexadecimal representation preceded by a percent sign.

URI Escaping Process Original String Hello World! escape Escaped String Hello%20World%21 Common Escapes space ? %20 ! ? %21 Safe Characters A-Z, a-z, 0-9 - . _ ~

Example

Let us see an example demonstrating basic URL escaping −

using System;

public class Demo {
   public static void Main() {
      string URI1 = "https://www.tutorialspoint.com/index.htm";
      Console.WriteLine("URI = " + URI1);
      string URI2 = "https://www.tutorialspoint.com/";
      Console.WriteLine("URI = " + URI2);
      Console.WriteLine("\nEscaped string (URI1) = " + Uri.EscapeDataString(URI1));
      Console.WriteLine("Escaped string (URI2) = " + Uri.EscapeDataString(URI2));
   }
}

The output of the above code is −

URI = https://www.tutorialspoint.com/index.htm
URI = https://www.tutorialspoint.com/
Escaped string (URI1) = https%3A%2F%2Fwww.tutorialspoint.com%2Findex.htm
Escaped string (URI2) = https%3A%2F%2Fwww.tutorialspoint.com%2F

Using EscapeDataString for Query Parameters

The most common use case is escaping user input for URL query parameters −

using System;

public class QueryParameterExample {
   public static void Main() {
      string userName = "John Doe";
      string userEmail = "john.doe@example.com";
      string searchQuery = "C# programming & development";
      
      Console.WriteLine("Original values:");
      Console.WriteLine("Name: " + userName);
      Console.WriteLine("Email: " + userEmail);
      Console.WriteLine("Search: " + searchQuery);
      
      Console.WriteLine("\nEscaped for URL parameters:");
      Console.WriteLine("Name: " + Uri.EscapeDataString(userName));
      Console.WriteLine("Email: " + Uri.EscapeDataString(userEmail));
      Console.WriteLine("Search: " + Uri.EscapeDataString(searchQuery));
      
      // Building a complete query string
      string queryString = "name=" + Uri.EscapeDataString(userName) + 
                          "&email=" + Uri.EscapeDataString(userEmail) + 
                          "&search=" + Uri.EscapeDataString(searchQuery);
      
      Console.WriteLine("\nComplete query string:");
      Console.WriteLine(queryString);
   }
}

The output of the above code is −

Original values:
Name: John Doe
Email: john.doe@example.com
Search: C# programming & development

Escaped for URL parameters:
Name: John%20Doe
Email: john.doe%40example.com
Search: C%23%20programming%20%26%20development

Complete query string:
name=John%20Doe&email=john.doe%40example.com&search=C%23%20programming%20%26%20development

Common Use Cases

  • Query Parameters: Escaping user input before adding to URL query strings

  • API Calls: Preparing data for REST API endpoints that require URL encoding

  • Form Data: Encoding form field values for HTTP POST requests

  • Path Components: Safely including dynamic content in URL paths

Conclusion

The Uri.EscapeDataString() method is essential for safely including user data and special characters in URLs. It ensures proper URL encoding by converting unsafe characters to percent-encoded sequences, making it indispensable for web development and API integration scenarios.

Updated on: 2026-03-17T07:04:35+05:30

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements