How To: Add Dynamic Data to an Existing Web Site

This topic describes how to modify an existing ASP.NET Web site to include Dynamic Data framework features. A good approach to incorporating Dynamic Data into an exiting Web site is to first make an ASP.NET Dynamic Data Web site that has the database that you want to use. You can then copy the Dynamic Data files and settings to the exiting Web site. This topic describes how to perform this task. It assumes that you have a working Dynamic Data Web site whose features you can copy to the existing ASP.NET Web site.

Note

In this topic, the exiting Web site without Dynamic Data features is referred to as the target Web site.

To add Dynamic Data files to a Web site

  1. Copy the DynamicData folder from an existing Dynamic Data Web site to the root of the target Web site.

    Note

       If you do not want the DynamicData folder in the root of the Web site, you can use a different location. For more information, see How To: Change the Dynamic Data Folder Location.

  2. If the target Web site does not contain an App_Code folder in the root directory, in Solution Explorer, right-click the Web site name, click Add ASP.NET Folder, and then click App_Code.

  3. Copy the contents of the App_Code folder from the Dynamic Data Web site to the target Web site.

  4. In Solution Explorer, right-click the App_Code folder, and then click Add Existing Item.

  5. In the Add Existing Item dialog box, add the files that you copied from the App_Code folder of the existing Dynamic Data Web site. Verify that the file mask in the drop-down list is All Files (*.*).

    This step adds the files to the solution. (Simply copying the files as you did in step 3 does not add them to the solution.)

  6. If the Web site uses SQL Server Express Edition with a local .mdf file, do the following:

    • In Solution Explorer for the target Web site, right-click the App_Data folder, and then click Add Existing Item.

    • In the Add Existing Item dialog box, enter the location of the existing .mdf file, and then click Add. This creates a copy of the database file in the target Web site.

      Note

      If you are using a remote database, you must instead update the connection string in the Web.config file, as shown later in this topic.

  7. Copy the Site.master file, Site.master code-behind file, and the Site.css file from the Dynamic Data Web site to the root directory of the target Web site. Add these files to the Visual Studio solution as you did in step 5.

To update the Global.asax file for Dynamic Data

  1. If the target Web site does not contain a Global.asax file, copy the Global.asax file from the Dynamic Data Web site to the target Web site, and then add the Global.asax file to the Visual Studio solution.

  2. If the target Web site already contains a Global.asax file, do the following:

    1. Copy the RegisterRoutes method from the Dynamic Data Global.asax file to the target Web site's Global.asax file.

    2. Add the RegisterRoutes method call in the Application_Start method.

To configure the target Web site

  1. In the target Web site, open the Web.config file.

  2. Copy the add child element of the connectionStrings element to the target Web.config file.

  3. Add assembly references for the following assemblies:

    • System.Web.Abstractions

    • System.Web.Routing

    • System.ComponentModel.DataAnnotations

    • System.Web.DynamicData

    • System.Data.Linq

    The following example shows a snippet from the Web.config file that contains the assemblies section.

    <system.web>
      <compilation>
        <assemblies>
    
          <add assembly="System.Web.Abstractions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
        <add assembly="System.Web.Routing, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
         <add assembly="System.ComponentModel.DataAnnotations, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
          <add assembly="System.Web.DynamicData, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
          <add assembly="System.Data.Linq, Version=3.5.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089"/>
    
        </assemblies>
      </compilation>
    
    <system.web>
    
  4. If the Dynamic Data Web site uses the ADO.NET Entity Framework, do the following:

    1. In the compilation section, add a buildProviders element just under the assemblies section that you updated in the previous step.

    2. In the buildProviders element, add an add element, set its extension attribute to ".edmx", and set its type to the provider for the ADO.NET Entity Framework.

    The following example shows a snippet from the Web.config file that contains the buildProviders section.

    <system.web>
      <compilation>
        <assemblies>
        </assemblies>
        <buildProviders>
          <add extension=".edmx" 
            type="System.Data.Entity.Design.AspNet.EntityDesignerBuildProvider"/>
        </buildProviders>
      </compilation>
    
    <system.web>
    
  5. Configure the asp tag prefix to reference the System.Web.DynamicData namespace, as shown in the following example:

    <system.web>
      <pages>
        <controls>
          …   
          <add tagPrefix="asp" namespace="System.Web.DynamicData" 
             assembly="System.Web.DynamicData, Version=3.5.0.0, 
             Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
        </controls>
      </pages>
    </system.web>
    
  6. In the httpModules section, add an add element, set its name to "UrlRoutingModule", and set its additional attributes as shown in the following example:

    <httpModules>
        <add name="ScriptModule" type="System.Web.Handlers.ScriptModule, 
               System.Web.Extensions, Version=3.5.0.0, Culture=neutral, 
               PublicKeyToken=31BF3856AD364E35"/>
        <add name="UrlRoutingModule" 
               type="System.Web.Routing.UrlRoutingModule, 
               System.Web.Routing, Version=3.5.0.0, 
               Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
    </httpModules>
    
  7. Modify the modules element by doing the following:

    • In the modules element, add the runAllManagedModulesForAllRequests attribute and set its value to "true".

    • Add a remove element and set its name attribute to "UrlRoutingModule".

    • Add an add element, set its name attribute to "UrlRoutingModule", and set its attributes as shown in the following example.

    The following example shows a snippet from the Web.config file that contains the completed modules section.

    <system.webServer>
      <validation validateIntegratedModeConfiguration="false"/>
      <modules runAllManagedModulesForAllRequests="true">
        <remove name="ScriptModule"/>
        <remove name="UrlRoutingModule"/>
        <add 
          name="ScriptModule" 
          preCondition="managedHandler" 
          type="System.Web.Handlers.ScriptModule, System.Web.Extensions, 
             Version=3.5.0.0, Culture=neutral, 
             PublicKeyToken=31BF3856AD364E35"/>
        <add name="UrlRoutingModule" 
          type="System.Web.Routing.UrlRoutingModule, System.Web.Routing, 
             Version=3.5.0.0, Culture=neutral, 
             PublicKeyToken=31BF3856AD364E35"/>
      </modules>
    
    </system.webServer>
    
  8. In the handlers element, add an add element, set its name to "UrlRoutingModule", and set its additional attributes as shown in the following example:

    <system.webServer>
    
      <handlers>
    
        <add 
          name="UrlRoutingHandler" 
          preCondition="integratedMode" 
          verb="*" path="UrlRouting.axd" 
          type="System.Web.HttpForbiddenHandler, System.Web, 
              Version=2.0.0.0, Culture=neutral, 
              PublicKeyToken=b03f5f7f11d50a3a"/>
      </handlers>
    </system.webServer>
    
  9. Save and close the Web.config file.

See Also

Tasks

How To: Change the Dynamic Data Folder Location

Change History

Date

History

Reason

July 2008

Added topic.

SP1 feature change.