Udostępnij za pośrednictwem


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

This walkthrough explains how to convert an existing Visual Studio Web site project to a Visual Studio Web application project. For more information about these project types, see Web Application Projects versus Web Site Projects in Visual Studio.

Tasks illustrated in this walkthrough include the following:

  • Opening and verifying the Web site project.

  • Creating a new 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 must have the following:

  • Visual Studio or Visual Web Developer Express

  • .NET Framework 2.0 or a later version.

Opening and Verifying the Web Site Project

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

To open and verify the Web project

  1. Open the existing solution.

  2. In the File menu, click Open 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.

    Note

    If the Debugging Not Enabled dialog box is displayed, select Modify the Web.config file to enable debugging, and then choose OK. This sets the debug attribute of the compilation element in the Web.config file to true. Make sure that you change this setting back to false before you deploy the project to production.

  6. Verify that your project runs as expected.

Creating a New Web Application Project

The best strategy for converting an existing Web site project is to first create a new, blank Web application project in a separate directory. This avoids changing any part of the existing Web site project 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 Web application project in a new solution

  1. In Visual Studio, close all open solutions.

  2. In the File menu, click New 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 Empty Web Application.

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

Setting Project References

If the Web site project requires additional project or assembly references, you can add them to the Web application project.

To help prevent errors, before you convert the project files, add references to the Web application project for assemblies that are in the Bin folder in the Web site project.

For more information about references, see Managing Project References.

To add references to a Web application project

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

    The Reference Manager 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 Web site project directory to the Web application project directory.

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 then click Copy.

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

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

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

  7. Select the new files in Solution Explorer.

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

One difference between a Web site project and a Web application project is that in a Web site project, ASP.NET dynamically generates partial classes for ASP.NET Web Forms pages and user controls. 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 to recursively examine every page, master page, and user control in the project, and to automatically generate a .designer.cs or .designer.vb file for each. Visual Studio also changes the .aspx or .ascx files to use the codeBehind attribute instead of the codeFile attribute. This conversion process 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.

    Note

    If the message "This action will add designer and code behind files for the selected items, which is required for converting Web site project files to Web application project files. Do you want to continue?" is displayed, click Yes.

  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 dynamically compiles all classes in the App_Code folder, you should not store classes in the folder that you compile as part of your Web application project. If you do, the class will be compiled two times. The first time will be as part of the Visual Studio 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 uses IIS Express 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 Tab, Project Properties.

Additional Conversion Options

There are the following conversion options to consider when you convert your Web site project to a Web application project:

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

Note

If you must update the namespace multiple times in a file, you can use the 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 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 element for 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.

    Note

    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

Profile properties enable you to store and retrieve user-profile data in a personalization database. In 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 more information about Profile properties, see ASP.NET Profile Properties Overview.

The 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);
            }
        }
    }
}
    Imports System 
    Imports System.Collections.Generic 
    Imports System.Text 
    Imports System.Web 
    Imports System.Web.Profile 

    Namespace WebApplication1 
        Public Class ProfileCommon
            Public Property Teachers() As Teachers
                Get
                    Return DirectCast(HttpContext.Current.Profile.GetPropertyValue("Teachers"), Teachers)
                End Get
                Set(ByVal value As Teachers)
                    HttpContext.Current.Profile.SetPropertyValue("Teachers", value)
                End Set
            End Property
        End Class
    End Namespace

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();
        }
    }
    Public Partial Class _Default 
        Inherits System.Web.UI.Page 
        Public Profile As New ProfileCommon() 
        Protected Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs) 
            Dim teachers As New Teachers() 
            teachers.Add(New Teacher("scott")) 
            teachers.Add(New Teacher("bob")) 
            teachers.Add(New Teacher("paul")) 
            
            Profile.Teachers = teachers 
        End Sub 
        Protected Sub Button2_Click(ByVal sender As Object, ByVal e As EventArgs) 
            GridView1.DataSource = Profile.Teachers 
            GridView1.DataBind() 
        End Sub 
    End Class 

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.

See Also

Tasks

How to: Use Surround-with Code Snippets

Reference

Web Tab, Project Properties

@ Page

Concepts

Managing Project References

ASP.NET Profile Properties Overview

Other Resources

ASP.NET Web Projects

Web Application Projects versus Web Site Projects in Visual Studio

ASP.NET Web Application Projects

Web Project Conversion from Visual Studio .NET