A couple of other interesting changes to the ASP.NET Dynamic Data Websites Framework found in the latest Dynamic Data Preview on Code Gallery that differs from the previous drop that I presentated to the Sarasota .NET Developer Group.
Registering the LINQ To SQL DataContext as the Model
Previously in ASP.NET Dynamic Data Previews, the DataContext to be registered as the Data Model was either magically found via reflection ( if only one ) or registered im the web.config file. As you can guess, this was rather limiting as it did not give you the flexibility of specifying the DataContext programmatically at run-time. The good news is that they have now changed the way the DataContext in LINQ To SQL is registered, you now specifiy it programmatically at run-time.
Here is a bit of code showing the simple registration of the DataContext Class in the Global.asax file:
public static void RegisterRoutes(RouteCollection routes) {
MetaModel model = new MetaModel();
model.RegisterContext(typeof(NorthwindDataContext),
new ContextConfiguration() { ScaffoldAllTables = true });
}
void Application_Start(object sender, EventArgs e) {
RegisterRoutes(RouteTable.Routes);
}
Similar to the ASP.NET MVC Framework there is a RegisterRoutes Method that is called from Application_Start. By default, there is now code in the RegisterRoutes Method for specifying the LINQ To SQL DataContext to be used for the model as well as to set the ever important ScaffoldAllTables = true. Note that in the case of the code above, I added NorthwindDataContext as the type of DataContext.
Interesting enough, if you instead build your ASP.NET Dynamic Data Website using the new ASP.NET Dynamic Data Wizard Project Template, the registration of the model is a bit different, but logically the same:
public static void RegisterRoutes(RouteCollection routes) {
MetaModel model = new MetaModel();
Register_NorthwindDataContext(model);
}
private static void
Register_NorthwindDataContext(MetaModel model)
{
ContextConfiguration config = new ContextConfiguration();
config.ScaffoldAllTables = true;
model.RegisterContext(typeof(NorthwindDataContext), config);
}
void Application_Start(object sender, EventArgs e) {
RegisterRoutes(RouteTable.Routes);
}
Routing via System.Web.Routing
Speaking of routing as mentioned above, there is an obvious similarity in routing between the ASP.NET Dynamic Data Framework and MVC Framework, since both use the new System.Web.Routing Assembly to handle routing. The new routing mechanism as compared to ASP.NET Webforms will probably take a bit getting use to as the path to your pages is no longer physical, but completely logical.
Here is a bit of code from the Global.asax file in a ASP.NET Dynamic Data Website associated with routing:
public static void RegisterRoutes(RouteCollection routes) {
routes.Add(new DynamicDataRoute("{table}/ListDetails.aspx") {
Action = PageAction.List,
ViewName = "ListDetails",
Model = model
});
routes.Add(new DynamicDataRoute("{table}/ListDetails.aspx") {
Action = PageAction.Details,
ViewName = "ListDetails",
Model = model
});
}
void Application_Start(object sender, EventArgs e) {
RegisterRoutes(RouteTable.Routes);
}
In this case model is the MetaModel instance we specified above as being the LINQ To SQL DataContext class called NorthwindDataContext. I could be wrong, but I don't believe routing has changed from previous versions of ASP.NET Dynamic Data. I just thought it was worth mentioning for those of you not using the ASP.NET MVC Framework that this new System.Web.Routing Assembly is shared between Dynamic Data and the MVC Framework, giving you much more flexibility in the routing scheme you use in your web applications.
Conclusion
So now when you want to set the LINQ To SQL DataContext Class as your data model and enable scaffolding of all tables do not look in the web.config. Instead, check the global.asax file.
Hope this helps.
Dave
ASP.NET Dynamic Data Tutorials