Click here to Skip to main content
6,630,586 members and growing! (17,599 online)
Email Password   helpLost your password?
Web Development » ASP.NET » Samples     Intermediate License: The Code Project Open License (CPOL)

A small Class for simplifying the Work with URL Parameters

By Uwe Keim

An article describing a simple class that makes working with URL parameters a little bit easier.
C#.NET 1.0, .NET 1.1, .NET 2.0, Win2K, WinXP, Win2003, DotGNU, ASP.NET, Visual Studio, Dev
Posted:13 Dec 2004
Updated:5 Jan 2005
Views:85,041
Bookmarked:70 times
Announcements
Loading...
 
Search    
Advanced Search
Add to IE Search
printPrint   add Share
      Discuss Discuss   Broken Article?Report  
27 votes for this article.
Popularity: 6.47 Rating: 4.52 out of 5
1 vote, 3.7%
1

2
1 vote, 3.7%
3
7 votes, 25.9%
4
18 votes, 66.7%
5

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

License

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

About the Author

Uwe Keim


Member
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 is also teached programming to students at the local university.

In his free time, he does climbing, running and mountain biking. You can watch him most of the day (and probably night) programming.

Some cool, free software from us:

Zeta Test - Integrated test management environment.
Zeta Producer - Intuitive Content Management System (CMS) for Windows.
Zeta Uploader - Easily send large files by e-mail.

Occupation: Software Developer
Company: zeta software GmbH
Location: Germany Germany

Other popular ASP.NET articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 25 of 35 (Total in Forum: 35) (Refresh)FirstPrevNext
GeneralThanks PinmemberColinBashBash12:02 26 May '09  
GeneralRe: Thanks PinsitebuilderUwe Keim12:04 26 May '09  
QuestionCompatibility and optimizations PinmemberPeterB723:52 25 May '09  
GeneralGreat work PinmemberShahedul Huq Khandkar22:39 24 Jul '06  
GeneralRe: Great work PinsitebuilderUwe Keim22:44 24 Jul '06  
GeneralDo you have it in vb.net? PinmemberHamidTheProgrammer7:55 20 Sep '05  
AnswerRe: Do you have it in vb.net? PinsitebuilderUwe Keim9:18 20 Sep '05  
GeneralRe: Do you have it in vb.net? PinmemberGavin Harriss16:35 19 Feb '07  
GeneralA better C# to VB.NET converter PinmemberGavin Harriss16:54 19 Feb '07  
GeneralPageMethods helps with URLs PinsussAnonymous15:00 17 May '05  
GeneralRe: PageMethods helps with URLs PinsitebuilderUwe Keim19:30 17 May '05  
Generalquerystring and postback Pinmembermagister4:54 11 May '05  
GeneralRe: querystring and postback PinsitebuilderUwe Keim19:25 11 May '05  
GeneralQuite a useful class, done something similar PinmemberSam Collett0:41 11 May '05  
GeneralRe: Quite a useful class, done something similar PinsitebuilderUwe Keim0:58 11 May '05  
GeneralGreat work PinmemberNoman Nadeem21:23 25 Apr '05  
GeneralRe: Great work PinsitebuilderUwe Keim2:29 28 Apr '05  
GeneralHow to retrieve only the parameters ? PinmemberJohann Frot6:42 6 Apr '05  
GeneralRe: How to retrieve only the parameters ? PinsitebuilderUwe Keim18:00 6 Apr '05  
GeneralRe: How to retrieve only the parameters ? PinmemberJohann Frot21:46 6 Apr '05  
GeneralPerfect! PinmemberIPC20000:07 31 Mar '05  
GeneralRe: Perfect! PinsitebuilderUwe Keim1:05 31 Mar '05  
GeneralValidation PinsussYussef4:50 18 Jan '05  
GeneralRe: Validation PinsitebuilderUwe Keim18:25 18 Jan '05  
Generalretrieving passed param values from redirected page Pinmembermisha20004:40 6 Jan '05  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 5 Jan 2005
Editor: Sumalatha K.R.
Copyright 2004 by Uwe Keim
Everything else Copyright © CodeProject, 1999-2009
Web18 | Advertise on the Code Project