Information
The topic you requested is included in another documentation set. For convenience, it's displayed below. Choose Switch to see the topic in its original location.

Walkthrough: Converting a Web Site Project to a Web Application Project in Visual Studio

This walkthrough explains how to convert an existing Microsoft Visual Studio 2005 Web site project to a Microsoft Visual Studio 2005 Web application project. The Web application project model uses the same conceptual approach as a Web project in Visual Studio .NET 2003. This includes a project file to include and exclude files, and compilation to a single assembly. For more information, see Web Project Conversion from Visual Studio .NET.

NoteNote:

You can create ASP.NET Web application projects using Visual Studio or Visual Web Developer Express.

Tasks illustrated in this walkthrough include the following:

  • Opening and verifying the Visual Studio 2005 Web site project.

  • Creating a new Visual Studio 2008 Web application project.

  • Setting project references.

  • Copying files to the new Web application project.

  • Converting the project files.

  • Running the Web application project.

  • Adding namespace syntax.

  • Converting declarative typed datasets.

  • Converting profile object code.

In order to complete this walkthrough, you will need:

  • Visual Studio 2008 or Microsoft Visual Studio 2005 Service Pack 1 (SP1) or Visual Web Developer Express

  • .NET Framework version 2.0.

Before you convert a project, you should verify that it is working correctly. This will prevent errors during conversion.

To open and verify your Visual Studio 2005 Web project

  1. Open the existing Visual Studio 2005 solution.

  2. In the File menu, click Open, and then click Web Site.

    The Open Web Site dialog box is displayed.

  3. Select the project folder that you want to open, and then click Open.

  4. In the Build menu, click Build Web Site.

  5. In the Debug menu, click Start Debugging. Alternatively, you can press F5.

    NoteNote:

    If the Debugging Not Enabled dialog box is displayed, select Add a new Web.config file with debugging enabled.

  6. Verify that your project runs as expected.

The best strategy for converting an existing Visual Studio 2005 Web site project is to first create a new, blank Visual Studio 2008 Web application project in a separate directory. This avoids changing any part of the existing Web site files. It also enables you to copy existing functionality into the new Web application project.

You can add the new project to an existing solution, which is ideal when you have several class-library projects that you want to use. Alternatively, you can start a new instance of Visual Studio and create a new solution and project.

To create a new Visual Studio 2008 Web application project in a new solution

  1. In Visual Studio 2008, close all open solutions.

  2. In the File menu, click New, and then click Project.

    The New Project dialog box is displayed.

  3. In the Project types section of the New Project dialog box, expand the language that you want to use, and then select Web to display the Web-related templates.

  4. Select ASP.NET Web Application.

  5. Type values for Name, Location, and Solution Name, and then click OK to create the Web application project.

  6. After the project has been created, delete the Default.aspx and Web.config files that are created automatically.

Setting Project References

If the Visual Studio 2008 Web site project requires additional project or assembly references, you can add them to the Web application project. You can see the list of default references with new Visual Studio 2008 Web application projects under the References node in Solution Explorer.

NoteNote:

To help prevent errors, before you convert the project files, add references to the Web application project for assemblies that existed in the Bin folder. See the following procedure for more information about how to add references.

For more information about references, see Project References.

To add references to a Visual Studio 2008 Web application project

  1. In Solution Explorer, right-click References, and then click Add Reference.

    The Add Reference dialog box is displayed.

  2. Select the reference that you want to add and then click OK.

  3. In Solution Explorer, right-click the Web application and click Build.

    Visual Studio builds the project and verifies that any project-to-project references are working.

Copying Files to the Web Application Project

The easiest way to add your existing files is to copy the files from a Visual Studio 2008 Web site project directory to the Web application project directory.

NoteNote:

If the Data Source Configuration Wizard starts during the copy process, click Cancel in the wizard's dialog box and let the rest of the files be copied.

To copy a Web site project to a Web application project

  1. In Solution Explorer, right-click the Web site project and select Open Folder in Windows Explorer.

  2. Select the files of the Web site project to copy.

  3. Right-click the selected files and select Copy.

  4. In the Web application project, right-click the Web application project and select Open Folder in Windows Explorer.

  5. Paste the Web site project files into the Web application directory.

  6. In Solution Explorer of the Web application project, click the Show All Files button,

  7. Select the new files in the Solution Explorer.

  8. Right-click the selected files and then click Include In Project.

One difference between a Visual Studio 2008 Web site project and a Visual Studio 2008 Web application project is that the Web site project model dynamically generates the tool-generated partial class.

After you copy the files from the Web site project to the Web application project, you will notice that the code-behind files for each page and user-control are still associated with the .aspx, .master, and .ascx files. No .designer.cs or .designer.vb files have been generated. As part of the next step, you convert these pages to save their partial classes in a .designer.cs or .designer.vb file.

Converting the Project Files

Visual Studio includes an option to convert pages and classes in Web application projects to use partial classes. Partial classes are used to separate the markup in a page or user control code-behind code. These designer-generated classes are stored in a separate file from the code-behind file.

The conversion process causes Visual Studio 2008 to recursively examine every page, master page, and user control in the project, and to automatically generate a .designer.cs file for each. Visual Studio also changes the .aspx or .ascx files to use the codeBehind attribute instead of the codeFile attribute. This command also renames the App_Code folder to Old_App_Code.

To convert pages and classes to use partial classes in a Web application project

  1. In Solution Explorer, right-click the root project folder that contains the pages and classes that you want to convert, and then click Convert to Web Application.

  2. Build the project to see whether there are any compilation errors.

If you see errors, the two most likely causes are as follows:

  • An assembly reference that is missing and that must be added to your project.

  • An issue with a dynamically generated type, such as the Profile object or a typed dataset.

If you are missing an assembly reference, open the reference manager and add it. If you are using a dynamically generated type, see Converting Declarative Typed Datasets and Converting Profile Object Code later in this topic.

Because ASP.NET 2.0 dynamically compiles all classes in the App_Code folder, you should not store classes in the folder that you compile as part of your Visual Studio 2008 Web application. If you do, the class will be compiled two times. The first time will be as part of the Visual Studio 2008 Web application project assembly, and the second time will be at run time by ASP.NET. This can cause a "could not load type" exception, which occurs because there are duplicate type names in the application.

Instead, store class files in any folder of your project other than the App_Code folder. This is done automatically by the Convert to Web Application command, which renames the folder to Old_App_Code.

Running the Web Application Project

You can now compile and run the application. By default, Visual Studio 2008 uses the built-in ASP.NET Development Server to run the site. Alternatively, you can configure the project to use Internet Information Services (IIS). To manage Web application project settings, right-click the project and then click Properties. You can then select the Web tab to configure these run-time settings.

For more information, see Web Page, Project Designer.

There are additional conversion options to consider when you convert your project to a Web application project. These options are as follows:

  • Adding namespace syntax.

  • Converting declarative typed datasets.

  • Converting profile object code.

Adding Namespace Syntax

By default, pages and classes that are built by using the Visual Studio 2008 Web site project model do not automatically include a code namespace. However, pages, controls, and classes that are built by using the Visual Studio 2008 Web application project model automatically include a code namespace. When converting the Web site project to a Web application project, you will have to add the namespaces to the code.

For instance, when you work with C#, you can add namespaces to code in existing classes within Visual Studio by using the Surround With shortcut menu command in the C# code editor.

To add a namespace to existing classes

  1. Open the code file that you want to modify.

  2. Select a class (or multiple classes) in the source editor, right-click the selected code, and then click Surround With.

  3. Select the Namespace item in the list.

Files with the extensions .aspx, .ascx, .master, .ashx, .asmx, and .asax contain directives (for example, an @ Page directive) with Inherits or Class attributes that list the class names that they invoke if they are code-behind pages. If you add namespaces to the code-behind files of these file types, you must also add the namespace to the Inherits and Class declarations. For example, if you add the WebApplication5 namespace to the code-behind file of a page named Details.aspx, you also change the Inherits attribute of the @ Page directive from inherits=Details_aspx to inherits=WebApplication5.Details_aspx.

NoteNote:

If you must update the namespace multiple times in a file, you can use find-and-replace feature of the Visual Studio editor.

For more information about how to use the Surround With command, see How to: Use Surround-with Code Snippets.

Converting Declarative Typed Datasets

If the Visual Studio 2008 Web site project contains strongly typed dataset classes in the App_Code folder, you must make an additional change to fix the connectionString element in the Web.config file. You must also reset the connectionString element for each TableAdapter object in the dataset class.

To fix the connectionString of strongly typed datasets

  1. In Solution Explorer, right-click the dataset class in the App_Code folder and then click View Designer.

    The Designer window is displayed for the dataset.

    NoteNote:

    If the dataset connection is configured, you can double-click the dataset to display it in the designer.

  2. Right-click a TableAdapter object and then click Properties.

  3. Select the Connection property and reset the connection by selecting an available option.

    When you update the connection, the connection string reference is also updated.

  4. Repeat these steps for each TableAdapter object in each dataset.

Converting Profile Object Code

ASP.NET 2.0 added support for profile properties. This feature enables you to store and retrieve user-profile data in a personalization database. In Visual Studio 2008 Web site projects, ASP.NET automatically adds a Profile object (an instance of the ProfileCommon class) to each ASP.NET Web page in the project. This object provides strongly typed access to all the properties that are defined in the profile section of the application's Web.config file. You can get IntelliSense data for this object, and automatically save and retrieve values from it.

For example, an application's Web.config file might contain the following section.

<profile defaultProvider="AspNetSqlProfileProvider">
  <properties>
    <add name="Teachers"
      type="Teachers"
      allowAnonymous="true" />
  </properties>
</profile>

In this case, you could add the following code to a page to save and retrieve information about the Teachers object, as shown in the following example.

public partial class _Default : System.Web.UI.Page 
{
    protected void Button1_Click(object sender, EventArgs e)
    {
        Teachers teachers = new Teachers();
        teachers.Add(new Teacher("Scott"));
        teachers.Add(new Teacher("Bob"));
        teachers.Add(new Teacher("Paul"));
        
        Profile.Teachers = teachers;
    }
    protected void Button2_Click(object sender, EventArgs e)
    {
        GridView1.DataSource = Profile.Teachers;
        GridView1.DataBind();
    }
}

The Visual Studio 2008 Web application project does not automatically include the ProfileCommon class. However, you can create your own ProfileCommon class that contains strongly typed properties for the items configured in the profile system. Then you can access the current Profile property of the HttpContext object to get and set the properties. The following example shows how to create a custom ProfileCommon class contained in Class1.cs (Class1.vb for Visual Basic) file.

using System;
using System.Collections.Generic;
using System.Text;
using System.Web;
using System.Web.Profile;

namespace WebApplication1
{
    public class ProfileCommon
    {
        public Teachers Teachers
        {
            get
                {
                return (Teachers) HttpContext.Current.Profile.GetPropertyValue("Teachers");
                }
            set
            {
        HttpContext.Current.Profile.SetPropertyValue("Teachers",value);
            }
        }
    }
}

You can then add an instance of the ProfileCommon class named Profile to the pages that must use the profile system, as shown in the following example.

    public partial class _Default : System.Web.UI.Page
    {
        ProfileCommon Profile = new ProfileCommon();
        protected void Button1_Click(object sender, EventArgs e)
        {
            Teachers teachers = new Teachers();
            teachers.Add(new Teacher("scott"));
            teachers.Add(new Teacher("bob"));
            teachers.Add(new Teacher("paul"));

            Profile.Teachers = teachers;    
        }
        protected void Button2_Click(object sender, EventArgs e)
        {
            GridView1.DataSource = Profile.Teachers;
            GridView1.DataBind();
        }
    }

You will not have to change any other code on the page and you will be able to use the profile system as you did with your original Web site project. For more information, see ASP.NET Profile Properties Overview.

Was this page helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

Show:
© 2014 Microsoft