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.

1244 ❘ ChaPTer 42 Asp.net dynAmic dAtA And mvc<br />

Both dynamic data <strong>and</strong> MVC make use of another methodology that was introduced in .<strong>NET</strong> 3.5, routing.<br />

This makes it possible for URLs to be simplifi ed <strong>and</strong> to include additional information, <strong>and</strong> is the fi rst thing<br />

you will look at in this chapter. After routing, this chapter moves on to dynamic data <strong>and</strong> MVC in turn,<br />

with a fi nal section about how to combine these technologies.<br />

Both dynamic data <strong>and</strong> MVC are large subjects to cover <strong>and</strong> would require multiple<br />

chapters to cover in depth. This chapter gives a general overview of using these<br />

frameworks, which will hopefully give you enough information to get started before<br />

fi n d i ng m o re i n - d e pt h re so u rc e s i f y o u wa n t t o u s e t he m i n m o re d e pt h.<br />

rouTing<br />

ASP.<strong>NET</strong> routing is a technology that enables you to assign sensible, human - readable URLs to web pages.<br />

Essentially, this means that the URL for a web page that a user sees in the address bar of a browser doesn ’ t<br />

necessarily match the physical location of a web page. Instead, the URL will include information that your<br />

web site can use to map to a page that you defi ne, <strong>and</strong> can pass parameters to that page.<br />

At fi rst glance, this may seem a little counterintuitive. Why wouldn ’ t you want users to see the address of<br />

a web page Surely not exposing this information will make it more diffi cult to locate resources. In fact,<br />

as you will see in this section, mapping URLs can make it much easier for users, <strong>and</strong> it has several other<br />

benefi ts as well.<br />

As an example, consider the following URL:<br />

http://www.myecommercesite.com/products/kites/reddragon<br />

Without knowing anything about the web site that uses this URL, you can probably take a good guess at<br />

what you ’ d fi nd on the page. From the words in the URL, you would expect to see a product page for a kite<br />

called “ Red Dragon. ”<br />

The advantages of this type of URL are as follows:<br />

➤<br />

➤<br />

➤<br />

➤<br />

Users can see at a glance what pages are.<br />

URLs are easy for users to remember, even without creating bookmarks.<br />

Users can modify URLs manually to navigate (for example, a user might replace the last section of the<br />

above URL with /products/balloons).<br />

Search engine results are optimized for these descriptive URLs, so the page ranking will be higher.<br />

There are essentially two ways to implement URLs of this form. You can either create a page for every possible<br />

URL, or you can use a framework that maps URLs to pages that can display data for multiple URLs.<br />

This fi rst possibility is worth mentioning, as it isn ’ t necessarily out of the question — it is possible<br />

to create static HTML sites that are generated from back - end data. However, this requires a fair amount of<br />

maintenance <strong>and</strong>, in most situations, can be more trouble than it ’ s worth. Now that the tools have matured<br />

the second scenario is more practical.<br />

In fact, ASP.<strong>NET</strong> has included the capability to use URLs like this for some time, although “ URL rewriting<br />

” (as it was known in earlier versions of ASP.<strong>NET</strong>) required an in - depth knowledge of the ASP<br />

.<strong>NET</strong> architecture <strong>and</strong> could be quite tricky to implement. With the advent of ASP.<strong>NET</strong> routing, things<br />

have become much easier.<br />

In this section, you will look at the following:<br />

➤<br />

Query string parameters — Before looking at routing specifi cally, it is worth underst<strong>and</strong>ing the case<br />

for including additional data in URLs in more detail. Query string parameters provide an alternative<br />

way of doing this, <strong>and</strong> have existed since the very beginnings of the web. However, while simpler than<br />

routing, they do not include all of the advantages.<br />

www.it-ebooks.info

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

Saved successfully!

Ooh no, something went wrong!