ASP.NET Dynamic Data

ASP.NET Dynamic Data is a feature that's often been ignored by ASP.NET MVC developers because it's a Web Forms feature. True, it is built on top of Web Forms, but that's really just an implementation detail. ASP.NET MVC and Web Forms are all ASP.NET applications and can be intermingled in productive ways.

For the NuGet Gallery, we decided to use Dynamic Data as an extremely fast way to build out a scaffolded administration UI so that we could edit data in the database via a browser. Eventually, we hope to build out a proper administration section to manage the gallery, but Dynamic Data works great in a pinch. Because this is an admin page, the details of the UI weren't important to us, though Dynamic Data is certainly customizable if we wanted to build a fancier UI.

To see Dynamic Data in action, make sure the Website project is set as the startup project and hit Ctrl+F5 to start it up in the browser. Append /dbadmin to the URL to visit the database admin site. Since you're running the site on the local host, it doesn't require that your user account is a member of the “Admins” role. It only requires that you are authenticated. So, register an account and then visit /dbadmin. When running remotely, the /dbadmin section also requires that you be a member of the Admins role.

You should see a list of every table in the database, as shown in Figure 16.3. Technically, not every table is listed, just those that correspond to an Entity Framework entity.

When you develop locally, make sure to click on Users. If you are just starting out, you should be the only user in the database; otherwise, you can search for yourself by e-mail address. Once you find your user account, check the Roles checkbox labeled Admins, as shown in Figure 16.4, and then click Update to save your changes. You'll need this later when we cover ELMAH.

Adding Dynamic Data to an existing ASP.NET MVC application takes a few steps. I've encouraged the ASP.NET team to write a NuGet package to do this. But, for now, you'll need to do the following:

1. Create a new ASP.NET Dynamic Data Entities Web Application project. You won't need to keep this project around. You're just going to scavenge it for some files.
2. Copy the DynamicData folder from this project into your ASP.NET MVC project.
3. Copy the following files from the root of the Dynamic Data project into the DynamicData folder in your ASP.NET MVC project:
  • Default.aspx and Default.aspx.cs
  • Site.master and Site.master.cs
  • Site.css
  • Web.config
4. Fix all the file references:
a. In Site.master, change the link tag's href value from ∼/site.css to site.css. This makes sure the Dynamic Data templates use the site.css in the DynamicData folder.
b. In Site.master, change the anchor tag's href value from ∼/to ∼/dbadmin (or wherever you intend to serve the Dynamic Data admin).
c. Change the MasterPageFile directive in every page of the DynamicDataPageTemplates folder to point from ∼/Site.master to Site.master. This ensures the Dynamic Data templates use the master page within the DynamicData folder.
5. Use NuGet to install the DynamicData.EFCodeFirstProvider package. This will add the reference to the System.Web.DynamicData assembly.
6. Register the Dynamic Data model provider, the Entity Framework context, and routes. Listing 16-2 shows an example of code that registers the code that enables Dynamic Data. This is pulled from the Register.cs class in the NuGet Gallery.

Listing 16.2 : Listing 16-2: Dynamic Data Registration

using System.Web.DynamicData;
using System.Web.Routing;
using DynamicData.EFCodeFirstProvider;
using NuGetGallery;

namespace DynamicDataEFCodeFirst
{
    public class Registration
    {
        private static readonly MetaModel _defaultModel = 
            new MetaModel();

        public static MetaModel DefaultModel
        {
            get
            {
                return _defaultModel;
            }
        }

        public static void Register(RouteCollection routes)
        {
            DefaultModel.RegisterContext(
               new EFCodeFirstDataModelProvider(
                   () => new EntitiesContext()),
                   new ContextConfiguration() 
            { ScaffoldAllTables = true });

            // This route must come first to prevent some other route 
            // from the site to take over
            routes.Insert(0, new DynamicDataRoute("dbadmin/{table}/{action}")
            {
                Constraints = new RouteValueDictionary(new 
                    { action = "List|Details|Edit|Insert" }),
                Model = DefaultModel
            });

            routes.MapPageRoute(
                "dd_default",
                "dbadmin",
                "∼/DynamicData/Default.aspx");
        }
    }
}

Make sure to call the Register method when your application starts up. The AppActivator.cs code calls this method for the NuGet Gallery.

As I write this, David Ebbo is working on a NuGet package to automate these steps, so keep an eye out for it.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset