Click here to Skip to main content
13,298,818 members (38,095 online)
Click here to Skip to main content
Add your own
alternative version


46 bookmarked
Posted 2 Dec 2003

HttpUrlQuery Library

, 7 Dec 2003
Rate this:
Please Sign up or sign in to vote.
A library for manipulating standard HTTP query strings. Very useful in dynamic applications where query string manipulation usually produces very ugly code that takes attention from developing main logic of an application.


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.


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"];
/* or more briefly, but less effective for repeat operations: */
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.


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.
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
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.


This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


About the Author

Alex Beynenson
Web Developer
United States United States
No Biography provided

You may also be interested in...


Comments and Discussions

GeneralPageMethods helps with URLs Pin
nbenin17-May-05 15:03
membernbenin17-May-05 15:03 
GeneralRe: PageMethods helps with URLs Pin
nbenin26-Nov-05 15:05
membernbenin26-Nov-05 15:05 
GeneralVery Very Nice Pin
JerSchneid4-Dec-04 17:32
memberJerSchneid4-Dec-04 17:32 
GeneralOverload Pin
MorningZ12-Dec-03 7:12
memberMorningZ12-Dec-03 7:12 
GeneralRe: Overload Pin
Alex Beynenson12-Dec-03 8:52
memberAlex Beynenson12-Dec-03 8:52 
GeneralRe: Overload Pin
Rocky Moore14-Dec-03 11:47
memberRocky Moore14-Dec-03 11:47 
GeneralRe: Overload Pin
Alex Beynenson14-Dec-03 13:10
memberAlex Beynenson14-Dec-03 13:10 
GeneralRe: Overload Pin
Rocky Moore14-Dec-03 21:07
memberRocky Moore14-Dec-03 21:07 
GeneralRe: Overload Pin
Alex Beynenson15-Dec-03 4:18
memberAlex Beynenson15-Dec-03 4:18 
GeneralRe: Overload Pin
Spiff Dog22-Oct-04 7:07
memberSpiff Dog22-Oct-04 7:07 
GeneralProcedure for use Pin
MorningZ10-Dec-03 4:32
memberMorningZ10-Dec-03 4:32 
GeneralRe: Procedure for use Pin
Alex Beynenson10-Dec-03 4:55
memberAlex Beynenson10-Dec-03 4:55 
GeneralRe: Procedure for use Pin
MorningZ10-Dec-03 11:26
memberMorningZ10-Dec-03 11:26 
GeneralRe: Procedure for use Pin
Alex Beynenson10-Dec-03 11:32
memberAlex Beynenson10-Dec-03 11:32 
GeneralRe: Procedure for use Pin
Anonymous19-Apr-04 7:01
sussAnonymous19-Apr-04 7:01 
QuestionProxy support ? Pin
darthmaul10-Dec-03 0:25
memberdarthmaul10-Dec-03 0:25 
AnswerRe: Proxy support ? Pin
Alex Beynenson10-Dec-03 4:45
memberAlex Beynenson10-Dec-03 4:45 
GeneralGood concept; a few suggestions... Pin
Matt Sollars3-Dec-03 9:34
memberMatt Sollars3-Dec-03 9:34 
GeneralRe: Good concept; a few suggestions... Pin
Alex Beynenson3-Dec-03 10:31
memberAlex Beynenson3-Dec-03 10:31 
GeneralRe: Good concept; a few suggestions... Pin
Matt Sollars3-Dec-03 10:36
memberMatt Sollars3-Dec-03 10:36 
GeneralDownload is missing Pin
davidM3-Dec-03 8:58
memberdavidM3-Dec-03 8:58 
GeneralRe: Download is missing Pin
Alex Beynenson3-Dec-03 10:24
memberAlex Beynenson3-Dec-03 10:24 
GeneralRe: Download is missing Pin
davidM3-Dec-03 12:04
memberdavidM3-Dec-03 12:04 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.171207.1 | Last Updated 8 Dec 2003
Article Copyright 2003 by Alex Beynenson
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid