ASP.NET Dynamic Data uses routing to match http requests with the appropriate data, actions and views to produce the output (read only, read only details, edit and insert pages) that is expected for each table in the model. The routing system found in Dynamic Data is based on the ASP.NET routing system and gives you the ability to define the URL structure that you want to have. This URL structure doesn’t have to match the physical directory structure of the project and can be defined any way you choose.
Routing Basics
ASP.NET routing allows you to define URL pattern rules that suit your needs and make the URL a first class part of the application that the user can interact with directly and easily. When a page is requested, the URL is checked against a route table, and if there’s a match then a route handler is invoked for that request. The route handler then returns an IHttpHandler for rendering the page output.
In short, we can now have URls laid out the way we want such as http://example.com/products/beverages or http://example.com/employees/rachelappel.
You can find routing classes in the System.Web.Routing.dll with the same named namespace.
Dynamic Data Routing
A subset of the ASP.NET routing system is used to create routes for your Dynamic Data applications. In ASP.NET Dynamic Data, the physical directory structure looks like this by default:
The \DynamicData\PageTemplates\ directory is where the browsable ASP.NET web forms are located, however we don’t browse these pages directly as we would browse pages in a regular project. We can start by browsing the default.aspx page and then navigating using the links rendered in default.aspx. Once we’ve navigated using the hyperlinks in default.aspx (or using the browser’s address bar) we can see that the URL structure is different than the project’s structure.
When Browsing Products:
Products listing: http://localhost/dynamicdata/products/list.aspx
Edit a product: http://localhost/dynamicdata/products/edit.aspx?productid=1
Product details: http://localhost/dynamicdata/products/details.aspx?productid=1
When Browsing Employees:
Employees listing: http://localhost/dynamicdata/employees/list.aspx
Edit an employee: http://localhost/dynamicdata/employees/edit.aspx?productid=1
Employee details: http://localhost/dynamicdata/employees/details.aspx?productid=1
Rather than match the project’s folder structure when a URL is requested, Dynamic Data combines entities in the data model plus the defined route patterns in the Global.asax.cs file. Notice that each URL above contains the name of the entity (table) plus an action such as list, edit or details, and the action is the actual page.
To set up this routing pattern in Dynamic Data, we must first have and register a data model in the Global.asax.cs file.
model.RegisterContext(typeof(NorthwindDataContext),
new ContextConfiguration() { ScaffoldAllTables = true });
In this case a variable named model is registered to use a LINQ to SQL data model[1] that is available in the project and tells Dynamic Data to use (scaffold/template) all the tables in that model. Once your data model is registered, it can be used it to help define routes.
routes.Add(new DynamicDataRoute("{table}/{action}.aspx") { Constraints = new RouteValueDictionary(new { action = "List|Details|Edit|Insert" }), Model = model });
The {table}/{action} placeholders denote the pattern that the routing system will use. The placeholders match each entity that is defined in the data model with a stated action defined in the route to produce the mapped URLs. The action is what maps out to the physical file, such as List.aspx, Edit.aspx, Details.aspx or Insert.aspx . This syntax is a very concise way to setup all the routes together at once, by adding a RouteValueDictionary object and its associated actions to the Constraints of the DynamicDataRoute. The model is also passed as an argument of the Add method.
If we need more control over the specific routes, or want a read only site, the routes can be individually added. For example, if only the list route is added, no editing will be allowed throughout the site. This can be done by adding a new instance of a DynamicDataRoute object to the routes collection, and initializing it with an action, view and model.
routes.Add(new DynamicDataRoute("{table}/List.aspx") { Action = PageAction.List, ViewName = "List", Model = model });
The code above creates and adds a single route that displays the List.aspx page when any table in the model is requested for viewing in the form of /table/List.aspx. If we want to add another route for editing, almost identical code can be added except for a few changes.
- Set the PageAction enum equal to the appropriate PageAction value such as [PageAction.Edit, PageAction.Insert, etc…
- Set the ViewName equal to “Page” where “Page” is the .aspx page name without the extension.
Add in this code for each route you want to use such as edit, insert or details. If you don’t want to do any customizing or don’t need finely tuned control then either code sample can be used to define routes, by adding in a RouteValueDictionary or individual DynamicDataRoute objects, as this is a matter of choice.
Summary
The routing system in ASP.NET Dynamic Data is the basis for creating the dynamic system by combining the data model with a URL schema defined by the application. This routing system is highly customizable so that pretty URLs, SEO friendly URLs or a URL schema can be created that meets your business needs rather than just using the out of the box routes.
[1] The data model could just as easily be Entity Framework, LINQ to SQL or any data model that is supported in the future.



