Tuesday, November 5, 2024

Querystring Issue on C# with special characters

 If you're building a URL string dynamically in C#, you can use Uri.EscapeDataString to ensure proper encoding:

string searchTerm = "rock&roll";

string query = $"https://example.com/search?query={Uri.EscapeDataString(searchTerm)}";


This approach will automatically encode any special characters, including &, resulting in a safe and valid query string.