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.

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

Configuring routing<br />

One of the most important concepts to grasp when dealing with dynamic data sites is that pages are<br />

generated according to actions. An action is a way of defining what a page should do in response to, for<br />

example, the user clicking on a particular link. There are four page actions that are defined for you by<br />

default: List, Details, Edit, <strong>and</strong> Insert.<br />

Each of the page templates defined for a dynamic data site (which are also known as views) can function<br />

differently according to what action is currently being performed. The routing configuration for the web<br />

site associates actions with views, <strong>and</strong> each route can optionally be constrained by the tables that it should<br />

apply to. For example, you might create a new view that is intended for listing customers. That view might<br />

function differently from the default List.aspx view. To create the new view, you must configure routing<br />

so that the correct view will be used.<br />

The default routing for a dynamic data web site is configured in Global.asax as follows:<br />

routes.Add(new DynamicDataRoute("{table}/{action}.aspx")<br />

{<br />

Constraints = new RouteValueDictionary(<br />

new { action = "List|Details|Edit|Insert" }),<br />

Model = DefaultModel<br />

});<br />

code snippet PCSDynamicDataDemoEntities/Global.asax <strong>and</strong> PCSDynamicDataDemoLinq/Global.asax<br />

This uses the routing framework described earlier in the chapter, although here routes use the<br />

DynamicDataRoute type. This class derives from Route <strong>and</strong> provides specialized functionality for dealing<br />

with actions, views, <strong>and</strong> tables.<br />

You will notice in this code that this route includes the name of a table <strong>and</strong> the name of an action — where<br />

the value of the action is constrained to the four predefined page action types. To take the example of using<br />

a different view for listing customers, you might add the following route (before the existing one, or that one<br />

would take precedence):<br />

routes.Add(new DynamicDataRoute("Customers/List.aspx")<br />

{<br />

Table = "Customers",<br />

Action = PageAction.List,<br />

ViewName = "ListCustomers",<br />

Model = DefaultModel<br />

});<br />

This route associates the /Customers/List.aspx URL with the view ListCustomers.aspx, <strong>and</strong> so for<br />

this code to work, you must supply a file of this name in the PageTemplates directory. You can also see that<br />

the Table <strong>and</strong> Action properties are specified here, as they are not available in the URL any more. The way<br />

the dynamic data routing works is that {table} <strong>and</strong> {action} routing parameters are used to populate the<br />

Table <strong>and</strong> Action properties, <strong>and</strong> in this URL these parameters are not present.<br />

You can build up as complex a system of routing as you wish in this manner, providing specialized pages for<br />

tables <strong>and</strong> actions as you see fit. You can also make use of the ListDetails.aspx view, which is a masterdetail<br />

view of data that enables row selection <strong>and</strong> inline editing. To use this view, you can supply alternative<br />

routes, or simply uncomment the following routes that the dynamic data site template provides:<br />

//routes.Add(new DynamicDataRoute("{table}/ListDetails.aspx")<br />

//{<br />

// Action = PageAction.List,<br />

// ViewName = "ListDetails",<br />

// Model = DefaultModel<br />

//});<br />

www.it-ebooks.info

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

Saved successfully!

Ooh no, something went wrong!