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 2005 

This walkthrough explains the process of converting an existing Visual Studio 2005 Web site project to a 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 about Web project conversion, see Web Project Conversion from Visual Studio .NET.

Tasks illustrated in this walkthrough include the following:

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

  • Creating a new Visual Studio 2005 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.

Prerequisites

In order to complete this walkthrough, you will need:

  • Visual Studio 2005 Service Pack 1 (SP1)

  • The .NET Framework version 2.0

Opening and Verifying Your Visual Studio 2005 Web Site Project

Before converting a project, open your existing Visual Studio 2005 solution. Compile and run the solution to verify that everything works the way you want.

To open and verify your Visual Studio 2005 Web project

  1. On the File menu, click Open and then click Web Site.

  2. In the Open Web Site dialog box, select the project folder you want to open and then click Open.

  3. On the Build menu, select Build Web Site.

  4. On the Debug menu, select 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.

  5. Verify that your project runs as expected.

Creating a New Visual Studio 2005 Web Application Project

The best strategy for converting an existing Visual Studio 2005 Web site project is to first create a new, blank Visual Studio 2005 Web application project in a separate directory. This avoids changing any part of the existing Web site files, and will enable you to copy already implemented functionality into the new Web application project. You can add this new project either to your existing solution, which is ideal when you have several class-library projects that you want to use, or by starting a new instance of Visual Studio and creating a new solution and project within it.

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

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

  2. On the File menu, click New, and then click Project.The New Project dialog box opens.

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

  4. Select the ASP.NET Web Application template.

  5. Type the Name, Location, and Solution Name that you want to use, and then click OK to create the Web application project.

  6. Once the project is created, delete the Default.aspx and Web.config files.

Setting Project References

If your Visual Studio 2005 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 2005 Web application projects under the References node within Solution Explorer.

NoteNote

Before converting the project files, add references to the Web application project for assemblies that existed in the Bin folder to help prevent errors.

For more information about references, see Project References.

To add references to a Visual Studio 2005 Web application project

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

  2. In the Add Reference dialog box, 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 New Web Application Project

The easiest way to add your existing files is to copy the files from a Visual Studio 2005 Web site project to the Web application project. In Solution Explorer, select all the files in the Web site project and then copy and paste the files into the new Web application project. The directory structure should look the same as it did in the Web site project.

NoteNote

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

One difference between a Visual Studio 2005 Web site project and a Visual Studio 2005 Web application project is that the Visual Studio 2005 Web site project model dynamically generates the tool-generated partial class and does not persist it on disk. However, the Visual Studio 2005 Web application project model saves this partial class on disk within files that have a .designer.cs file name extension. Also, the Web application project model compiles the partial class using the in-memory Visual Studio compilers when a build occurs.

After copying the files from the Web site project to the Web application project, you will notice how the code-behind files for each page and user-control are still associated with the .aspx, .master, and .ascx files. No .designer.cs files have been generated. As part of the next step, you will be converting these pages to persist their partial class declarations on disk in a .designer.cs file.

Converting the Project Files

Visual Studio includes an option to convert pages and classes within Web application projects to use partial class declarations. Partial classes are used to separate designer-generated code from code-behind code. These designer-generated classes are stored in a separate file from the code-behind file. This conversion process causes Visual Studio 2005 to recursively examine every page, user-control, and master-page 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 will also rename the App_Code file to Old_App_Code.

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

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

After you have converted pages and classes to use partial classes, build the project to see whether there are any compilation errors. The two most likely causes of errors are:

  • A missing assembly reference 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, you should open the reference manager and add it. If you are using a dynamically generated type, see and later in this topic.

NoteNote

If a Visual Basic class file contains more than one namespace, make sure to verify each namespace after conversion.

Because ASP.NET 2.0 dynamically compiles any classes it finds in the App_Code directory of an application at run time, you should not store classes that you compile as part of your Visual Studio 2005 Web application project in the App_Code folder. If you do, the class will get compiled two times: one time as part of the Visual Studio 2005 Web application project assembly, and then again at run time by ASP.NET. The result will most likely be a could not load type runtime exception, which occurs because there are duplicate type names in the application. Instead, you should store your class files in any folder of your project other than the App_Code folder. This will be handled automatically by the Convert to Web Applicaiton command, which will rename the folder to Old_App_Code.

Running the Web Application Project

Once you have completed the previous steps, you will be able to compile and run your application without any errors. By default, Visual Studio will use the built-in Visual Studio 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 runtime settings. For more information about the Web application project Web tab, see Web Page, Project Designer.

Additional Conversion Options

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

  • Adding namespace syntax.

  • Converting declarative typed datasets.

  • Converting profile object code.

Adding Namespace Syntax

By default, pages and classes built by using the Visual Studio 2005 Web site project model do not automatically include a code namespace. However, pages, controls, and classes built by using the Visual Studio 2005 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.

You can easily 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. In Solution Explorer, select and 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.

Note that .aspx, .ascx, .master, .ashx, .asmx, and .asax files contain inherits or class attributes that list the class names they should invoke. So, 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 a namespace called "WebApplication5" to the code-behind file, then you would change the page directive of the Details.aspx page from "inherits=Details_aspx" to "inherits=WebApplication5.Details_aspx".

NoteNote

When converting the Web site project to a Web application project, verify that the Inherits attribute and the CodeFile attribute of the Page directive contain the namespace of the class to be inherited.

If you must update the namespace multiple times in a file, you can use the Find and Replace command on the Edit menu to avoid individually changing each instance. In the Find what text box, type the inherits=" string, and in the Replace with box, type the inherits="WebApplicationName. string, where "WebApplicationName" is the new namespace. Make sure that you include the trailing period (.) at the end of the replacement string because it will be prepended to the previous class name during the replacement process.

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

Converting Declarative Typed Datasets

If you have strongly typed DataSet classes in the App_Code directory in your Visual Studio 2005 Web site project, then you must make an additional change to fix the connectionString in the Web.config file. Specifically, you must open each DataSet in the Designer, select each TableAdapter, and then reset the connectionString for the object.

To fix the connectionString of strongly typed DataSets

  1. In Solution Explorer, right-click the DataSet and then select View Designer.The Designer window opens.

    NoteNote

    If the DataSet connection is configured, you can double-click the DataSet to display it in View Designer.

  2. Right-click each TableAdapter and select Properties.

  3. Select the Connection property and reset the connection by selecting an available drop-down option. When you update the connection, the ConnectionString will also be updated.

Converting Profile Object Code

ASP.NET 2.0 adds support for a new feature named profile personalization. This feature enables developers to easily store and retrieve user-profile data in a personalization database. With Visual Studio 2005 Web site projects, ASP.NET automatically adds a strongly typed Profile object to each page in the project. This object provides a strongly typed mapping of all properties defined in the profile section of the application's Web.config file. Developers can then 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, developers could add the following code to their pages to save and retrieve information about the "Teachers" object as shown in the following code 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();
    }
}

With the Visual Studio 2005 Web site project option, this scenario is supported because Visual Studio is dynamically creating and adding a ProfileCommon class named Profile to every code-behind instance.

The SP1 version of the Visual Studio 2005 Web application project does not automatically include the ProfileCommon class. Therefore, some additional steps are required to obtain strong-typing for the Profile class. Specifically, you can create your own ProfileCommon class that contains strongly typed properties for the items configured in the profile system. Then you can accesses the current Profile property of the HttpContext object to get and set the properties as shown in the following code example.

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

namespace WebApplication6
{
    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 code example.

namespace WebApplication20
{
    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. Also, you will be able to use the profile system as you did with your original Web site project. For more information about how to use the profile system, see ASP.NET Profile Properties Overview.

See Also

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

Community Additions

Show:
© 2014 Microsoft