Click here to Skip to main content
Licence 
First Posted 2 Dec 2003
Views 88,483
Bookmarked 46 times

HttpUrlQuery Library

By | 7 Dec 2003 | Article
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

About the Author

Alex Beynenson

Web Developer

United States United States

Member



Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralPageMethods helps with URLs Pinmembernbenin14:03 17 May '05  
Interesting work. Those who want to go further with URL handling in ASP.NET can take a look at PageMethods. PageMethods enables well-defined URLs using methods and attributes.
GeneralRe: PageMethods helps with URLs Pinmembernbenin14:05 26 Nov '05  
GeneralVery Very Nice PinmemberJerSchneid16:32 4 Dec '04  
GeneralOverload PinmemberMorningZ6:12 12 Dec '03  
GeneralRe: Overload PinmemberAlex Beynenson7:52 12 Dec '03  
GeneralRe: Overload PinmemberRocky Moore10:47 14 Dec '03  
GeneralRe: Overload PinmemberAlex Beynenson12:10 14 Dec '03  
GeneralRe: Overload PinmemberRocky Moore20:07 14 Dec '03  
GeneralRe: Overload PinmemberAlex Beynenson3:18 15 Dec '03  
GeneralRe: Overload PinmemberSpiff Dog6:07 22 Oct '04  
GeneralProcedure for use PinmemberMorningZ3:32 10 Dec '03  
GeneralRe: Procedure for use PinmemberAlex Beynenson3:55 10 Dec '03  
GeneralRe: Procedure for use PinmemberMorningZ10:26 10 Dec '03  
GeneralRe: Procedure for use PinmemberAlex Beynenson10:32 10 Dec '03  
GeneralRe: Procedure for use PinsussAnonymous6:01 19 Apr '04  
QuestionProxy support ? Pinmemberdarthmaul23:25 9 Dec '03  
AnswerRe: Proxy support ? PinmemberAlex Beynenson3:45 10 Dec '03  
GeneralGood concept; a few suggestions... PinmemberMatt Sollars8:34 3 Dec '03  
GeneralRe: Good concept; a few suggestions... PinmemberAlex Beynenson9:31 3 Dec '03  
GeneralRe: Good concept; a few suggestions... PinmemberMatt Sollars9:36 3 Dec '03  
GeneralDownload is missing PinmemberdavidM7:58 3 Dec '03  
GeneralRe: Download is missing PinmemberAlex Beynenson9:24 3 Dec '03  
GeneralRe: Download is missing PinmemberdavidM11:04 3 Dec '03  

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.

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