Click here to Skip to main content
11,438,636 members (77,112 online)
Click here to Skip to main content

A Small Class for Simplifying the Work with URL Parameters

, 4 Apr 2010 CPOL
Rate this:
Please Sign up or sign in to vote.
An article describing a simple class that makes working with URL parameters a little bit easier
An URL inside the Microsoft Internet Explorer Address bar

Introduction

(For the latest changes, please see the History section below.)

If you are working with URLs from within ASP.NET, you probably often need to read different URL parameters. E.g., you could read the value of the "name1" parameter out of the QueryString collection of your Page class (or, to be more precise, from the Request property of your Page class):

private void Page_Load(
    object sender, 
    System.EventArgs e )
{
    string value1 = Request.QueryString["name1"];
 
    // ... Further processing of the value1 variable ... 
}

In my projects, I often need the ability to write URL parameters, e.g., for outputting a URL on a page or for redirecting to another page with the appropriate parameters. This article introduces a class QueryString which I wrote in order to simplify my life a little bit.

Some Examples

Before examining the QueryString class in more detail, first some examples of how to use it are shown.

Example 1 - Manipulate an attribute value

Create a QueryString and simply let itself fill with the parameters of the current page:

private void Page_Load(
    object sender, 
    System.EventArgs e )
{
    // Let the object fill itself 
    // with the parameters of the current page.
    QueryString qs = new QueryString();
 
    // Read a parameter from the QueryString object.
    string value1 = qs["name1"];
  
    // Write a value into the QueryString object.
    qs["name1"] = "This is a value";
  
    // Redirect with the current content of the QueryString object.
    // In this example, since the BeforeUrl property is not modified,
    // it will redirect to the current page itself, but with the
    // "name1" parameter set to the new value.
    Response.Redirect( qs.All, true );
}

As you can see, you can simply modify a parameter's value by assigning it to the appropriate parameter name by using the [] operator. The All property returns the complete URL that is stored inside the QueryString object including the latest (possibly modified) parameters.

Example 2 - Removing parameters

To remove a certain parameter from the QueryString object, call the RemoveParameter method, specifying the name of the parameter to remove:

private void Page_Load(
    object sender, 
    System.EventArgs e )
{
    // Let the object fill itself 
    // with the parameters of the current page.
    QueryString qs = new QueryString();
 
    // Read a parameter from the QueryString object.
    string value1 = qs["name1"];
  
    // Now remove the parameter.
    qs.RemoveParameter( "name1" );
   
    // This has the same effect as RemoveParameter() method:
    qs["name1"] = null;
  
    // ... Further processing of the value1 variable ... 
}

The Class in Brief

The most common use of the class was already described in the previous section. It should be simple to use, n'est pas? Nevertheless, here is an (incomplete) overview of the most usable members:

Constructors

The following constructors exists.

  • public QueryString() - Empty constructor, fills itself with the parameters of the current page (if any)
  • public QueryString( System.Web.UI.Page currentPage ) - Constructs with the parameters of the given page
  • public QueryString( string url ) - Constructs with the parameters from the given URL
  • public QueryString( Uri uri ) - Constructs with the parameters from the given URI

Methods for reading from a given URL

By using the following methods, you can achieve similar results as with the constructors, but after the object is already constructed.

  • public void FromUrl( System.Web.UI.Page currentPage ) - Fills with the parameters of the given page
  • public void FromUrl( string url ) - Fills with the parameters from the given URL
  • public void FromUrl( Uri uri ) - Fills with the parameters from the given URI

Miscellaneous operations

Use the following methods for further operations.

  • public bool HasParameter( string parameterName ) - Check whether a given parameter is present (i.e. is non-null and non-empty-string)
  • public void RemoveParameter( string name ) - Removes a parameter from the current parameter collection. Does nothing if the parameter is not present
  • public void RemoveAllParameters() - Removes all parameters from the current parameter collection

Miscellaneous properties

Use the following properties for accessing various values.

  • public string this [string index] - Gets or sets a parameter value by the given parameter name. If the parameter does not exists, get returns string.Empty (i.e. not null).
  • public string BeforeUrl - Gets or sets the the "base" part of the URL returned by the All property. E.g. set this to "http://www.myserver.com/mypage.aspx"
  • public string All - Gets the complete URL that is currently stored inside the object. This is the value of the All property plus the current parameters. E.g. returns "http://www.myserver.com/mypage.aspx?name1=value1&name2=someOtherValue".

Conclusion

In this small article, I've shown you a class that simplifies the reading and writing of URL parameter values. For me, this class is around about approximately two years and helped me save a lot of coding time (well, at least a bit...). Hopefully it is useful for you, too.

For questions, comments and remarks, please use the commenting section at the bottom of this article.

History

  • 2010-04-05: Added new version that adds several new methods (like a HTTP 301 redirect, which is good for SEO) and makes use of the concept of a Fluent interface
  • 2005-01-06: Corrected some very small spelling errors
  • 2004-12-30: Added fix and suggestion from Chris Maunder
  • 2004-12-14: Created first version of article

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Uwe Keim
Chief Technology Officer Zeta Producer Desktop CMS
Germany Germany
Uwe does programming since 1989 with experiences in Assembler, C++, MFC and lots of web- and database stuff and now uses ASP.NET and C# extensively, too. He has also teached programming to students at the local university.

In his free time, he does climbing, running and mountain biking. Recently he became a father of a cute boy.

Some cool, free software from us:

German Developer Community  
Free Test Management Software - Intuitive, competitive, Test Plans. Download now!  
Homepage erstellen - Intuitive, very easy to use. Download now!  
Send large Files online for free by Email
Offline-Homepage-Baukasten
Follow on   Twitter   Google+   LinkedIn

Comments and Discussions

 
QuestionURL REDIRECTION Pin
Member 917112831-Jul-12 20:58
memberMember 917112831-Jul-12 20:58 
GeneralMy vote of 5 Pin
Sunasara Imdadhusen4-Jan-11 23:13
memberSunasara Imdadhusen4-Jan-11 23:13 
GeneralParameters Encode / Decode Pin
Member 32658742-Sep-10 0:50
memberMember 32658742-Sep-10 0:50 
GeneralRe: Parameters Encode / Decode Pin
Uwe Keim2-Sep-10 1:31
mvpUwe Keim2-Sep-10 1:31 
GeneralRe: Parameters Encode / Decode Pin
Member 32658742-Sep-10 5:51
memberMember 32658742-Sep-10 5:51 
GeneralRe: Parameters Encode / Decode Pin
Member 32658742-Sep-10 5:55
memberMember 32658742-Sep-10 5:55 
AnswerA way to remove parameter-name hardcode. Pin
valera.kolupaev10-Dec-09 1:54
membervalera.kolupaev10-Dec-09 1:54 
GeneralThanks Pin
ColinBashBash26-May-09 12:02
memberColinBashBash26-May-09 12:02 
GeneralRe: Thanks Pin
Uwe Keim26-May-09 12:04
sitebuilderUwe Keim26-May-09 12:04 
GeneralRe: Thanks Pin
Uwe Keim4-Apr-10 22:54
mvpUwe Keim4-Apr-10 22:54 
QuestionCompatibility and optimizations Pin
PeterB7225-May-09 3:52
memberPeterB7225-May-09 3:52 
GeneralGreat work Pin
Shahedul Huq Khandkar24-Jul-06 22:39
memberShahedul Huq Khandkar24-Jul-06 22:39 
GeneralRe: Great work Pin
Uwe Keim24-Jul-06 22:44
sitebuilderUwe Keim24-Jul-06 22:44 
QuestionDo you have it in vb.net? Pin
HamidTheProgrammer20-Sep-05 7:55
memberHamidTheProgrammer20-Sep-05 7:55 
AnswerRe: Do you have it in vb.net? Pin
Uwe Keim20-Sep-05 9:18
sitebuilderUwe Keim20-Sep-05 9:18 
AnswerRe: Do you have it in vb.net? Pin
Gavin Harriss19-Feb-07 16:35
memberGavin Harriss19-Feb-07 16:35 
GeneralA better C# to VB.NET converter Pin
Gavin Harriss19-Feb-07 16:54
memberGavin Harriss19-Feb-07 16:54 
GeneralPageMethods helps with URLs Pin
Anonymous17-May-05 15:00
sussAnonymous17-May-05 15:00 
GeneralRe: PageMethods helps with URLs Pin
Uwe Keim17-May-05 19:30
sitebuilderUwe Keim17-May-05 19:30 
Generalquerystring and postback Pin
magister11-May-05 4:54
membermagister11-May-05 4:54 
GeneralRe: querystring and postback Pin
Uwe Keim11-May-05 19:25
sitebuilderUwe Keim11-May-05 19:25 
GeneralQuite a useful class, done something similar Pin
Sam Collett11-May-05 0:41
memberSam Collett11-May-05 0:41 
GeneralRe: Quite a useful class, done something similar Pin
Uwe Keim11-May-05 0:58
sitebuilderUwe Keim11-May-05 0:58 
GeneralGreat work Pin
Noman Nadeem25-Apr-05 21:23
memberNoman Nadeem25-Apr-05 21:23 
GeneralRe: Great work Pin
Uwe Keim28-Apr-05 2:29
sitebuilderUwe Keim28-Apr-05 2:29 
QuestionHow to retrieve only the parameters ? Pin
Johann Frot6-Apr-05 6:42
memberJohann Frot6-Apr-05 6:42 
AnswerRe: How to retrieve only the parameters ? Pin
Uwe Keim6-Apr-05 18:00
sitebuilderUwe Keim6-Apr-05 18:00 
GeneralRe: How to retrieve only the parameters ? Pin
Johann Frot6-Apr-05 21:46
memberJohann Frot6-Apr-05 21:46 
GeneralPerfect! Pin
IPC200031-Mar-05 0:07
memberIPC200031-Mar-05 0:07 
GeneralRe: Perfect! Pin
Uwe Keim31-Mar-05 1:05
sitebuilderUwe Keim31-Mar-05 1:05 
GeneralValidation Pin
Yussef18-Jan-05 4:50
sussYussef18-Jan-05 4:50 
GeneralRe: Validation Pin
Uwe Keim18-Jan-05 18:25
sitebuilderUwe Keim18-Jan-05 18:25 
Generalretrieving passed param values from redirected page Pin
misha20006-Jan-05 4:40
membermisha20006-Jan-05 4:40 
GeneralRe: retrieving passed param values from redirected page Pin
Uwe Keim6-Jan-05 4:51
sitebuilderUwe Keim6-Jan-05 4:51 
GeneralRe: retrieving passed param values from redirected page Pin
misha20006-Jan-05 23:34
membermisha20006-Jan-05 23:34 
GeneralRe: retrieving passed param values from redirected page Pin
Uwe Keim7-Jan-05 8:26
sitebuilderUwe Keim7-Jan-05 8:26 
GeneralMinor bug fix Pin
Chris Maunder29-Dec-04 17:28
adminChris Maunder29-Dec-04 17:28 
GeneralRe: Minor bug fix Pin
Uwe Keim29-Dec-04 17:55
sitebuilderUwe Keim29-Dec-04 17:55 
GeneralRe: Minor bug fix Pin
Uwe Keim30-Dec-04 0:56
sitebuilderUwe Keim30-Dec-04 0:56 
GeneralGood timing! Pin
Chris Maunder26-Dec-04 0:32
adminChris Maunder26-Dec-04 0:32 
GeneralRe: Good timing! Pin
Uwe Keim26-Dec-04 19:05
sitebuilderUwe Keim26-Dec-04 19:05 
GeneralGood Idea! Pin
Webdiyer14-Dec-04 15:04
memberWebdiyer14-Dec-04 15:04 
GeneralRe: Good Idea! Pin
Uwe Keim14-Dec-04 18:27
sitebuilderUwe Keim14-Dec-04 18:27 

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
Web01 | 2.8.150506.1 | Last Updated 5 Apr 2010
Article Copyright 2004 by Uwe Keim
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid