HomeSearch

ASP.NET QueryString Examples

Use QueryString to handle queries to web pages with the ASP.NET framework.

QueryString. Web pages can be requested with query strings. The QueryString in ASP.NET accesses this information. When you load file.html?x=y, it parses "x" and "y". Most examples show one way of using this NameValueCollection. There is a faster way.

First, we see an .aspx Web Forms page that executes when the user accesses Default.aspx. The code here is the code-behind part, Default.aspx.cs. It is written in the C# programming language.

Start: To test the code, run the page in the web browser on the ASP.NET development server. It will be completely blank.

Note: Try adding the string "?param=dotnet" at the end of the URL. The Response.Write will be triggered.

Result: The Page prints the value of the param query, which is a string value. The screenshot shows the result.

Info: The QueryString was correctly detected. This is an older web browser.

QueryString example: C# using System; using System.Web.UI; public partial class _Default : Page { protected void Page_Load(object sender, EventArgs e) { string v = Request.QueryString["param"]; if (v != null) { Response.Write("param is "); Response.Write(v); } } }

Two parameters. To continue, we test two query string URL parameters. This is a fairly common requirement in development. You may have to use either one or both at once. The next example here has some inefficiencies but otherwise works well.

Tip: The query string specifies that the "param" query is equal to "first". And the "id" param is equal to "true".

QueryString example with multiple parameters: C# using System; using System.Web.UI; public partial class _Default : Page { protected void Page_Load(object sender, EventArgs e) { string v = Request.QueryString["param"]; if (v != null) { Response.Write("param is "); Response.Write(v); } string x = Request.QueryString["id"]; if (x != null) { Response.Write(" id detected"); } } } Test code with this url: ?param=first&id=true

NameValueCollection. The Request.QueryString collection is a NameValueCollection internally. QueryString is a property getter for an internal NameValueCollection. The NameValueCollection is a specialized collection that has a somewhat unusual implementation.

Quote: Represents a collection of associated String keys and String values that can be accessed either with the key or with the index.

NameValueCollection Class: Microsoft Docs

Info: In performance testing, we find that using the index to access a value in NameValueCollection is far faster than using the ["string"] syntax.

Page that uses HasKeys on QueryString: C# using System; using System.Web.UI; using System.Collections.Specialized; public partial class _Default : Page { protected void Page_Load(object sender, EventArgs e) { // 1 // Get collection NameValueCollection n = Request.QueryString; // 2 // See if any query string exists if (n.HasKeys()) { // 3 // Get first key and value string k = n.GetKey(0); string v = n.Get(0); // 4 // Test different keys if (k == "param") { Response.Write("param is " + v); } if (k == "id") { Response.Write("id is " + v); } } } }

Notes, example. This code gets a reference to the QueryString collection. In high-performance code, you want to avoid excessive property lookups. Next we use the HasKeys() method on QueryString.

Tip: This is useful for telling whether there are any query string keys available on the URL.

Next: We do two lookups on the NameValueCollection to get the first key and the first value.

Caution: Because we only access the first key and value, this code doesn't work for more than one key value pair.

NameValueCollection: The QueryString is simply an instance of NameValueCollection. So we should understand NameValueCollection in general.

NameValueCollection

Benchmark. Here I benchmarked the query string code examples. The benchmark simply reads the first query string key-value pair from the URL. My requirement was to accept only the first query string on a page.
Example that uses HasKeys, Method A: C# using System; using System.Web; using System.Web.UI; using System.Collections.Specialized; public partial class _Default : Page { protected void Page_Load(object sender, EventArgs e) { HttpRequest q = Request; NameValueCollection n = q.QueryString; if (n.HasKeys()) { string k = n.GetKey(0); if (k == "one") { string v = n.Get(0); } if (k == "two") { string v = n.Get(0); } } } } Example that uses QueryString syntax, Method B: C# using System; using System.Web; using System.Web.UI; using System.Collections.Specialized; public partial class _Default : Page { protected void Page_Load(object sender, EventArgs e) { HttpRequest q = Request; string v = q.QueryString["one"]; if (v != null) { } v = q.QueryString["two"]; if (v != null) { } } }

We find that when you only need to access the first query string key-value pair, using HasKeys, Get and GetKey is much faster than the QueryString["string"] approach. Here are the results in more detail.
No query string Method A: 0.08 s Method B: 1.75 s Matching query string Method A: 0.45 s Method B: 2.80 s No matching query string Method A: 0.46 s Method B: 1.73 s

Lazy. QueryString is not filled in a lazy way. By the time your HttpRequest is being used, QueryString has already been initialized with all the query key-value pairs. I looked at the locals in the Visual Studio debugger to find this.

Therefore, the analysis in this article is correct. And the faster method shown is considerably faster than the naive approach. This could help improve performance of many websites.Debugging

Note: When you use QueryString in Global.asax or another hot path in your code, it pays to optimize the logic.

And: When your website runs the code several times each second, you need it to be as fast as possible.

Here, we saw examples of Request.QueryString. We used the same code pattern that most ASP.NET tutorials use. But we also noted how to use other methods on QueryString to gain a substantial performance increase.

Tip: The internal operation of these objects, such as QueryString, is worth investigating.

Home
Dot Net Perls
© 2007-2020 Sam Allen. Every person is special and unique. Send bug reports to info@dotnetperls.com.