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.

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

Our list view is now implemented, with a minimum of effort.<br />

Note that none of the links provided are yet active. If you try to click on the first Details link, for example,<br />

the page will navigate to http://localhost:59360/Products/Details/1 <strong>and</strong> receive the following<br />

error message:<br />

The view 'Details' or its master was not found. The following locations were searched:<br />

~/Views/Products/Details.aspx<br />

~/Views/Products/Details.ascx<br />

~/Views/Shared/Details.aspx<br />

~/Views/Shared/Details.ascx<br />

What is happening here is that the view exposes a URL that maps to the Details() method in<br />

ProductsController, which you haven’t yet configured. You can put a breakpoint in this method <strong>and</strong><br />

refresh the page to confirm this if you like. This method currently returns an ActionResult using a call to<br />

View() with no parameters, which has no associated view defined, hence the error.<br />

Customizing asP.neT mVC applications<br />

You now have a functioning ASP.<strong>NET</strong> MVC application, although you certainly wouldn’t call it “fully”<br />

functioning. At the moment, you have created a simple list view for products, but as you saw at the end of<br />

the previous section, there are other views required in order to make things work properly. Also, navigating<br />

to a list of products by manually entering a URL is hardly a good way to navigate through a web site, so<br />

that needs fixing, too.<br />

In this section, you will look in a little more detail at how URLs are mapped to controller operations, <strong>and</strong><br />

then modify the PCSMVCMagicShop web application to add additional functionality for the Products page.<br />

Url operation routing<br />

Earlier, it was noted that the Global.asax file in your application configures routing. The contents of the<br />

code-behind for this file are as follows:<br />

public class MvcApplication : System.Web.HttpApplication<br />

{<br />

public static void RegisterRoutes(RouteCollection routes)<br />

{<br />

routes.IgnoreRoute("{resource}.axd/{*pathInfo}");<br />

}<br />

routes.MapRoute(<br />

"Default",<br />

// Route name<br />

"{controller}/{action}/{id}",<br />

// URL with parameters<br />

new { controller = "Home", action = "Index", id = "" } // Parameter defaults<br />

);<br />

}<br />

protected void Application_Start()<br />

{<br />

RegisterRoutes(RouteTable.Routes);<br />

}<br />

code snippet PCSMVCMagicShop/Global.asax.cs<br />

Much of this should look familiar from the “Routing” section at the start of this chapter. The<br />

IgnoreRoute() <strong>and</strong> MapRoute() methods are extension methods that ASP.<strong>NET</strong> MVC defines for you to<br />

make it easier to customize routes.<br />

You can see a single default route defined that takes the form {controller}/{action}/{id}. There are<br />

also default values provided for the three route parameters controller, action, <strong>and</strong> id of Home, Index, as<br />

well as an empty string. For the /Products route you entered to get to the products list, these parameters<br />

were set to Products, Index, <strong>and</strong> an empty string.<br />

www.it-ebooks.info

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

Saved successfully!

Ooh no, something went wrong!