15.02.2015 Views

C# 4 and .NET 4

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

outing ❘ 1245<br />

➤<br />

➤<br />

Defining routes — ASP.<strong>NET</strong> routing requires you to define the routes that are available in your web<br />

site so that they can be used.<br />

Using route parameters — Once a route has been detected in a URL, the target page can make use of<br />

routing parameters.<br />

The downloadable code for this chapter includes a simple solution called PCSRoutingDemo that will be<br />

referred to from this section to illustrate the techniques.<br />

query string Parameters<br />

While surfing the web, you’ve probably noticed that a lot of web pages include information in their URLs in<br />

addition to their location. For example, in an e-commerce web site, you might see a URL that looks a little<br />

like the following:<br />

http://www.myecommercesite.com/products.aspxid=4<br />

In this URL the target page is products.aspx, but after the identifier for this page, you can see additional<br />

information in the form of a query string. The character indicates the start of the query string, <strong>and</strong> the<br />

remainder of the URL consists of a name/value pair, with an equals character (=) separating the name (id)<br />

from the value (4). In fact, URLs can contain several query string name/value pairs, separated by & symbols.<br />

When users navigate to an ASP.<strong>NET</strong> page with a query string (perhaps by clicking on a link that you have<br />

rendered from database data), you can write code to render the page accordingly. In this example, you<br />

might use the value of the id query string parameter to extract data from a database, for a product with the<br />

corresponding ID. This means that you don’t have to create a page for every product in your database; you<br />

can, instead, create a single page that can show the details for any product.<br />

In practice, ASP.<strong>NET</strong> pages receive query string information in the form of a NameValueCollection object.<br />

This is passed in the QueryString property of the HttpRequest object that is available in code-behind<br />

through the inherited Request property. The query string collection is indexed by name <strong>and</strong> index, so to get<br />

the value of the id query string parameter shown above, you could use either of the following lines of code:<br />

or:<br />

string idValue = Request.QueryString[0];<br />

string idValue = Request.QueryString["id"];<br />

Note that all query string values are passed as strings, so to get an integer value for id you’d need to parse<br />

the value.<br />

The use of query string parameters is illustrated in the PCSRoutingDemo web site for this chapter. If you<br />

view the web site in a browser <strong>and</strong> click the “Navigation with query string” link, you will see the query<br />

string parameters displayed, as shown in Figure 42-1.<br />

figure 42-1<br />

www.it-ebooks.info

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!