Click here to Skip to main content
15,881,812 members
Articles / Web Development / ASP.NET
Article

HttpUrlQuery Library

Rate me:
Please Sign up or sign in to vote.
3.40/5 (15 votes)
7 Dec 20032 min read 108.8K   656   45   23
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:

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

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


Written By
Web Developer
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralPageMethods helps with URLs Pin
fabrice.marguerie17-May-05 14:03
fabrice.marguerie17-May-05 14:03 
GeneralRe: PageMethods helps with URLs Pin
fabrice.marguerie26-Nov-05 14:05
fabrice.marguerie26-Nov-05 14:05 
GeneralVery Very Nice Pin
JerSchneid4-Dec-04 16:32
JerSchneid4-Dec-04 16:32 
GeneralOverload Pin
MorningZ12-Dec-03 6:12
MorningZ12-Dec-03 6:12 
GeneralRe: Overload Pin
Alex Beynenson12-Dec-03 7:52
Alex Beynenson12-Dec-03 7:52 
GeneralRe: Overload Pin
Rocky Moore14-Dec-03 10:47
Rocky Moore14-Dec-03 10:47 
GeneralRe: Overload Pin
Alex Beynenson14-Dec-03 12:10
Alex Beynenson14-Dec-03 12:10 
GeneralRe: Overload Pin
Rocky Moore14-Dec-03 20:07
Rocky Moore14-Dec-03 20:07 
GeneralRe: Overload Pin
Alex Beynenson15-Dec-03 3:18
Alex Beynenson15-Dec-03 3:18 
GeneralRe: Overload Pin
Spiff Dog22-Oct-04 6:07
Spiff Dog22-Oct-04 6:07 
GeneralProcedure for use Pin
MorningZ10-Dec-03 3:32
MorningZ10-Dec-03 3:32 
GeneralRe: Procedure for use Pin
Alex Beynenson10-Dec-03 3:55
Alex Beynenson10-Dec-03 3:55 
GeneralRe: Procedure for use Pin
MorningZ10-Dec-03 10:26
MorningZ10-Dec-03 10:26 
GeneralRe: Procedure for use Pin
Alex Beynenson10-Dec-03 10:32
Alex Beynenson10-Dec-03 10:32 
GeneralRe: Procedure for use Pin
Anonymous19-Apr-04 6:01
Anonymous19-Apr-04 6:01 
QuestionProxy support ? Pin
darthmaul9-Dec-03 23:25
darthmaul9-Dec-03 23:25 
AnswerRe: Proxy support ? Pin
Alex Beynenson10-Dec-03 3:45
Alex Beynenson10-Dec-03 3:45 
GeneralGood concept; a few suggestions... Pin
Matt Sollars3-Dec-03 8:34
Matt Sollars3-Dec-03 8:34 
GeneralRe: Good concept; a few suggestions... Pin
Alex Beynenson3-Dec-03 9:31
Alex Beynenson3-Dec-03 9:31 
GeneralRe: Good concept; a few suggestions... Pin
Matt Sollars3-Dec-03 9:36
Matt Sollars3-Dec-03 9:36 
GeneralDownload is missing Pin
davidM3-Dec-03 7:58
davidM3-Dec-03 7:58 
GeneralRe: Download is missing Pin
Alex Beynenson3-Dec-03 9:24
Alex Beynenson3-Dec-03 9:24 
GeneralRe: Download is missing Pin
davidM3-Dec-03 11:04
davidM3-Dec-03 11: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.