How to parse a query string into a NameValueCollection in .NET
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
Parsing query strings is a routine task in .NET web development. Whether you are building an API, processing incoming HTTP requests, or extracting parameters from URLs in a utility application, you need a reliable way to break a query string like ?name=Alice&age=30 into its individual key-value pairs. The .NET framework provides built-in tools that handle URL decoding, duplicate keys, and edge cases, so you do not need to write a manual string-splitting parser. This guide covers the standard approaches in both .NET Framework and .NET Core/.NET 5+.
Using HttpUtility.ParseQueryString (.NET Framework)
The most straightforward method in .NET Framework is HttpUtility.ParseQueryString, which lives in the System.Web namespace. It takes a query string and returns a NameValueCollection:
This method automatically handles URL decoding. For example, a query string containing city=New%20York will correctly decode the value to "New York".
Note that HttpUtility.ParseQueryString expects the query string without the leading ?. If your string starts with ?, strip it first:
Using QueryHelpers in ASP.NET Core
In ASP.NET Core and .NET 5+, the System.Web namespace is not available. Instead, use QueryHelpers.ParseQuery from Microsoft.AspNetCore.WebUtilities, which returns a Dictionary<string, StringValues>:
The StringValues type handles the case where a key appears multiple times in the query string, which is common for filter parameters and checkbox groups.
Handling Duplicate Keys
A query string can contain the same key more than once, such as color=red&color=blue. The NameValueCollection in .NET Framework handles this by storing comma-separated values:
In ASP.NET Core, StringValues natively supports multiple values without comma concatenation, making it more intuitive to work with.
Building Query Strings from a NameValueCollection
You can also go the other direction -- building a query string from a NameValueCollection. The ToString() method produces a properly encoded query string:
This is useful when constructing URLs programmatically, because it handles URL encoding for you automatically.
Accessing Query Strings in ASP.NET Controllers
In an ASP.NET Core controller, you can access query parameters directly from the Request object without manually parsing:
Model binding with [FromQuery] is the preferred approach in ASP.NET Core because it provides automatic type conversion and validation.
Common Pitfalls
- Forgetting to strip the leading question mark:
HttpUtility.ParseQueryString("?name=Alice")treats the?as part of the first key, resulting in a key named"?name"instead of"name". Always callTrimStart('?')on the input string before parsing. - Using System.Web in .NET Core projects: The
System.Webnamespace is not available in .NET Core or .NET 5+. Attempting to referenceHttpUtilitywithout adding theSystem.WebNuGet compatibility package will cause a compilation error. UseQueryHelpers.ParseQueryfromMicrosoft.AspNetCore.WebUtilitiesinstead. - Assuming single values for duplicate keys: If a query string contains
tag=a&tag=b, callingqueryParams["tag"]on aNameValueCollectionreturns"a,b"as a single string. If you split on commas naively, you may break values that legitimately contain commas. UseGetValues()to get a proper string array. - Not URL-encoding when building query strings: Manually concatenating query parameters with string interpolation skips URL encoding, which breaks values containing
&,=,#, or spaces. Always useHttpUtility.ParseQueryString(string.Empty)orQueryHelpers.AddQueryStringto build query strings safely. - Case sensitivity assumptions: Query parameter keys are case-sensitive by default in .NET. A request with
?Name=Alicewill not match a lookup forqueryParams["name"]. If you need case-insensitive lookups, convert keys to lowercase during parsing or use a case-insensitive dictionary.
Summary
- Use
HttpUtility.ParseQueryStringin .NET Framework to parse query strings into aNameValueCollection, andQueryHelpers.ParseQueryin .NET Core for aDictionary<string, StringValues>. - Both methods handle URL decoding automatically, so percent-encoded characters like
%20are converted to their proper values. - Handle duplicate keys with
GetValues()in .NET Framework or iterate overStringValuesin .NET Core. - Build query strings programmatically using
NameValueCollection.ToString()orQueryHelpers.AddQueryStringto ensure proper URL encoding. - In ASP.NET Core controllers, prefer
[FromQuery]model binding over manual query string parsing for cleaner, type-safe code.
Related reading
- How to parse a string into a nullable int
- How to parse strings to DateTime in C properly?
- How to pass parameters to ThreadStart method in Thread?
- How to pass parameters to ThreadStart method in Thread?
- How to pass Task results to other Tasks not using continuations
- How to play a sound in C, .NET
- How to preserve HttpContext in Web API async task
- How to prevent blank xmlns attributes in output from .NET's XmlDocument?

OOD Fundamentals
Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.