How to check that Request.QueryString has a specific value or not in ASP.NET?
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
In ASP.NET, Request.QueryString provides access to URL query parameters as a key-value collection. Checking whether a parameter exists and has a specific value is essential for routing logic, input validation, and conditional page behavior. The exact approach differs slightly between Web Forms, MVC, and ASP.NET Core.
ASP.NET Web Forms / Classic ASP.NET
Request.QueryString returns null when a key is missing, and an empty string when the key exists but has no value.
Distinguish Missing Key from Empty Value
ASP.NET MVC
In MVC controllers, query string values are accessible through Request.QueryString, action parameters, or model binding.
For parameters not bound to method arguments:
ASP.NET Core
ASP.NET Core provides HttpContext.Request.Query which is an IQueryCollection.
Preferred Approach: Model Binding
In ASP.NET Core, binding query parameters to action parameters is the cleanest pattern.
Handling Multiple Values for the Same Key
Query strings can contain duplicate keys (?tag=csharp&tag=dotnet).
Input Validation
Always validate query string values before using them in database queries or business logic.
Common Pitfalls
- Not checking for
nullbefore comparing values —Request.QueryString["missing"] == "value"returnsfalsesafely, but calling methods on null throwsNullReferenceException. - Confusing empty string (key present, no value) with null (key missing) — use
string.IsNullOrEmpty()to handle both. - Trusting query string values for security decisions without server-side validation — query parameters can be modified by users.
- Using
Request.QueryString["key"]in ASP.NET Core instead ofRequest.Query["key"]— the classicQueryStringproperty returns the raw string, not a parsed collection. - Not URL-decoding special characters — the framework handles this automatically, but manual string parsing can miss encoded values.
Summary
- Use
Request.QueryString["key"]in Web Forms and MVC,Request.Query["key"]in ASP.NET Core. - Check for
nullto detect missing keys,string.IsNullOrEmptyfor missing or empty values. - Prefer model binding (
[FromQuery]or action parameters) over manual query string access. - Always validate and sanitize query string values before using them in business logic or database queries.
- Use
GetValues()orStringValuesto handle multi-valued query parameters.

