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.