Introduction
If you've ever written a Web application even half-worth its byte-size in
usefulness, you've probably used the query string method of transferring data
between individual pages and scripts. In developing such an application, you may
have also noticed that the querystrings have to be crafted by hand, ensuring
that all standards are followed (such as concatenation of the field/values and
the leading ? character), not to mention the obligatory calls to
Server.UrlEncode()
-like functions to ensure your querystring will
be legible to a Web browser. This hand-crafting can lead to errors that
virtually cripple your application not to mention the visitor's experience.
Presented here is a fully object-oriented way of handling this querystring
mess.
The HttpUrlQuery
class introduces a light-weight layer over your
querystring operations that lets you manipulate individual fields and their
values in a clean familiar fashion, without writing unecessary code to ensure
their validity with the standards.
Updates
December 8, 2003
Added the Current
static property that gives you access to the
querystring contained in the current context, usually the current page. The
property returns a HttpUrlQuery
object making it just as simple to
work with. Example:
HttpUrlQuery cqs = HttpUrlQuery.Current;
string name = cqs["username"];
string name = HttpUrlQuery.Current["username"];
Note that the object returned by the Current
property uses
standard separator and leading strings, such as ? = and &
Should you need to modify these strings, you'll need to make a copy of the
object returned from the Current property, as shown in the first part of the
preceeding example.
Example
Declare a HttpUrlQuery
object, and instantiate it with contents
of current page's querystring. Note that this instantiation can also be done
from a variety of sources, including a NameValueCollection
, or
another HttpUrlQuery
object.
HttpUrlQuery qs = new HttpUrlQuery(Page.Request.QueryString);
Remove
an unneed field. No errors are caused if the field doesn't exist. If you need to
validate an existance of a field in the querystring, the Contains() method can
be used.
qs.Remove("name");
Setting the value of a field is quite easy.
If the field does not exist in the query string, it is appended to the end.
Otherwise, its value is simply modified to the new value. This example also
demonstrates encoding abilities of the library. If you do not desire the value
to be encoded, an overload of the
Set()
method exists that allows
you to do that.
qs.Set("name", "George Vorgen-Peterson");
The setting task can
also be accomplished using the following, more programmer-friendly syntax. The
same rules of the Set method apply.
qs["equation"] = "2 + 2 - 3 = 1";
Call the
ToString()
method of the object to get a string representation, which is preceeded
by the ? sign so it can be readily appended without any clumsy checks.
Response.Redirect("page.aspx" + qs.ToString();
Comments, questions, and suggestions are welcome. Enjoy.