Click here to Skip to main content
Click here to Skip to main content

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.

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"];
 
/* 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.

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.

License

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

Share

About the Author

Alex Beynenson
Web Developer
United States United States
No Biography provided

Comments and Discussions

 
GeneralPageMethods helps with URLs Pinmembernbenin17-May-05 15:03 
GeneralRe: PageMethods helps with URLs Pinmembernbenin26-Nov-05 15:05 
GeneralVery Very Nice PinmemberJerSchneid4-Dec-04 17:32 
GeneralOverload PinmemberMorningZ12-Dec-03 7:12 
GeneralRe: Overload PinmemberAlex Beynenson12-Dec-03 8:52 
GeneralRe: Overload PinmemberRocky Moore14-Dec-03 11:47 
GeneralRe: Overload PinmemberAlex Beynenson14-Dec-03 13:10 
GeneralRe: Overload PinmemberRocky Moore14-Dec-03 21:07 
GeneralRe: Overload PinmemberAlex Beynenson15-Dec-03 4:18 
GeneralRe: Overload PinmemberSpiff Dog22-Oct-04 7:07 
GeneralProcedure for use PinmemberMorningZ10-Dec-03 4:32 
GeneralRe: Procedure for use PinmemberAlex Beynenson10-Dec-03 4:55 
GeneralRe: Procedure for use PinmemberMorningZ10-Dec-03 11:26 
GeneralRe: Procedure for use PinmemberAlex Beynenson10-Dec-03 11:32 
GeneralRe: Procedure for use PinsussAnonymous19-Apr-04 7:01 
QuestionProxy support ? Pinmemberdarthmaul10-Dec-03 0:25 
AnswerRe: Proxy support ? PinmemberAlex Beynenson10-Dec-03 4:45 
GeneralGood concept; a few suggestions... PinmemberMatt Sollars3-Dec-03 9:34 
GeneralRe: Good concept; a few suggestions... PinmemberAlex Beynenson3-Dec-03 10:31 
GeneralRe: Good concept; a few suggestions... PinmemberMatt Sollars3-Dec-03 10:36 
GeneralDownload is missing PinmemberdavidM3-Dec-03 8:58 
GeneralRe: Download is missing PinmemberAlex Beynenson3-Dec-03 10:24 
GeneralRe: Download is missing PinmemberdavidM3-Dec-03 12:04 

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

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

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