15.02.2015 Views

C# 4 and .NET 4

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

MVC ❘ 1267<br />

At this point, hopefully, things should click into place. Prior to testing you added a controller called<br />

ProductsController, which had a method called Index(), which at the time was described as being the<br />

default controller method. Now you can deduce what actually happened when you entered the URL:<br />

1. The routing system interpreted the URL entered <strong>and</strong> assigned values to controller, action, <strong>and</strong> id<br />

as noted above.<br />

2. ASP.<strong>NET</strong> MVC searched for — <strong>and</strong> found — a controller for Products (note that the URL didn’t need<br />

to be /ProductsController, the Controller suffix is added automatically).<br />

3. ASP.<strong>NET</strong> MVC searched for — <strong>and</strong>, again, found — an action called Index, which is the Index()<br />

method in ProductsController.<br />

4. When calling Index(), ASP.<strong>NET</strong> MVC searched for — <strong>and</strong>, you guessed it, found — a view in the<br />

Products view folder capable of displaying a collection of Product objects.<br />

5. ASP.<strong>NET</strong> rendered the view.<br />

Similarly, when you subsequently attempted to view the details for an item, the appropriate method<br />

(Details()) was located after interpreting the URL route /Products/Details/1. The final parameter here<br />

was passed to the Details() method as an argument, although at this point things fell to pieces a bit as<br />

that method is not yet implemented <strong>and</strong> no view was available.<br />

This routing behavior, <strong>and</strong> the way that URLs are mapped directly to controller methods, is central to<br />

underst<strong>and</strong>ing ASP.<strong>NET</strong> MVC. It means that you have a direct route from a given URL into your code, <strong>and</strong> what<br />

you do in that code is, of course, entirely up to you. The “simple” operations, such as listing products <strong>and</strong> so on,<br />

are therefore just the start of what is possible; you can write any action you can conceive of in the same way.<br />

However, you must also consider other implications of this. You are in effect exposing your code, <strong>and</strong> the<br />

parameters passed to your code, directly to users. This means that defensive coding is essential, as who<br />

knows what parameters your methods might receive from people typing URLs manually.<br />

action links<br />

Now that you know how URLs are mapped to actions, it’s worth looking at how to include links in your<br />

application. You could just hard-code a link in your site, for example, the following to get to the Products<br />

page you added:<br />

Products<br />

However, this link makes assumptions about the site’s structure that aren’t necessarily permanent. If at<br />

some point you were to change the routing specification this link would break.<br />

Earlier in the chapter, you saw how the ASP.<strong>NET</strong> Routing system allows links to be dynamically generated<br />

according to routing parameters. Similarly, ASP.<strong>NET</strong> MVC allows links to be generated according to the<br />

controller, action, <strong>and</strong> additional values you want to use, as well as the text you want to display. This is<br />

achieved through the Html.ActionLink() extension method, which has a number of overloads, depending<br />

on what you want to achieve.<br />

For example, if you look in the Site.Master master page file in the Views\Shared folder, you will see the<br />

following code that defines the menu for the site:<br />

<br />

<br />

<br />

<br />

<br />

<br />

code snippet PCSMVCMagicShop/Views/Shared/Site.Master<br />

The overload used here has three string parameters, linkText, actionName, <strong>and</strong> controllerName.<br />

The first parameter specifies the text to display, <strong>and</strong> the other two specify the action <strong>and</strong> controller to<br />

www.it-ebooks.info

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

Saved successfully!

Ooh no, something went wrong!