Don’t Reinvent the Wheel — Use Built-in Utilities
Fermin Perdomo
November 1, 2025
I was working on adding a canonical URL structure in a project when I came across some old code that was manually parsing the query string.
Then I remembered — C# already provides a built-in utility for this:
Before:
queryParameters = new NameValueCollection();
var queryString = Request.QueryString.ToString();
if (!string.IsNullOrEmpty(queryString))
{
string[] querySegments = queryString.Split('&');
foreach (string segment in querySegments)
{
string[] parts = segment.Split('=');
if (parts.Length > 0)
{
string key = parts[0].Trim(new char[] { '?', ' ' });
string val = parts[1].Trim();
if (string.IsNullOrEmpty(queryParameters.Get(key)))
{
string decodedUrl = HttpUtility.UrlDecode(val);
val = decodedUrl.Split(',')[0];
queryParameters.Add(key, val);
}
}
}
}
After:
queryParameters = HttpUtility.ParseQueryString(Request?.Url?.Query ?? string.Empty);
With just one line of code, you can replace what used to take several lines of manual string splitting and decoding.
💡 Lesson learned: Don’t reinvent the wheel.
Always check if the language already provides a built-in, optimized function for what you’re trying to do.
It’s not PHP this time 😅 — but I thought it was worth sharing anyway.
Please login to leave a comment.