|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
|
Announcements
Chapters
Services
Feature Zones
|
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 " 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 Some ExamplesBefore examining the Example 1 - Manipulate an attribute valueCreate a 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 Example 2 - Removing parametersTo remove a certain parameter from the 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 briefThe 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: ConstructorsThe following constructors exists.
Methods for reading from a given URLBy using the following methods, you can achieve similar results as with the constructors, but after the object is already constructed.
Miscellaneous operationsUse the following methods for further operations.
Miscellaneous propertiesUse the following properties for accessing various values.
ConclusionIn 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
|
||||||||||||||||||||||