This article describes how to convert an existing Microsoft Visual Studio 2005 website project to a Microsoft Visual Studio 2005 web application project. The concept and method used by the web application project model are the same as those in Visual Studio. NET 2003. This conceptual approach includes a project file used to include the file and compilation in a single set of programs, or exclude them.
To complete the conversion, follow these steps:
Open and verify the Visual Studio 2005 web site project.
Create a New Visual Studio 2008 web application project.
Set project reference.
Copy the file to a new Web application project.
Convert the project file.
Run the web application project.
Add the namespace syntax.
Converts declarative and strongly-typed datasets.
Convert the configuration file object code.
To complete the conversion, you must:
Create a New Visual Studio 2008 Web Application Project
The best policy for converting existing Visual Studio 2005 website projects is to create a new blank Visual Studio 2008 web application project in a separate directory. This prevents changes to any part of the existing website file. It also facilitates copying existing functions to new web application projects.
You can add a new project to an existing solution. This is an ideal solution when there are multiple class library projects to be used. In addition, you can start a new instance of Visual Studio and create a new solution and project.
Create a Visual Studio 2008 web application project in the new solution
Disable all open solutions in Visual Studio 2008.
Click new in the File menu, and then click Project ".
The "New Project" dialog box is displayed.
In the "project type" section of the "Create Project" dialog box, expand the language to use, and select "Web" to display the web-related template.
Select ASP. NET web application ".
Type the values of "name", "location", and "solution name", and click "OK" to create a web application project.
After creating a project, delete the automatically created default. aspx and web. config files.
Copy a website project to a web application project
In Solution Explorer, right-click the website project and select Open folder in Windows Explorer ".
Select the file for which you want to copy the website project.
Right-click the selected file and select copy ".
In the web application project, right-click the web application project and select Open folder in Windows Resource Manager ".
Paste the website project file into the Web application directory.
In Solution Explorer of the Web application project, click show all files.
Select a new file in Solution Explorer.
Right-click the selected file and click include in project ".
One difference between a Visual Studio 2008 website project and a Visual Studio 2008 web application project is that the website project model dynamically generates a branch class generated by the tool.
After you copy a file from a website project to a Web application project, you will find that the code hidden file of each page and user control is still associated with the. aspx,. master, and. ascx files. The. designer. cs or. designer. vb file is not generated during this process. In the next step, you need to convert these pages to save their branch classes in the. designer. cs or. designer. vb file.
Convert project files
Visual Studio includes an option to convert pages and classes in a Web application project to use its partial classes. The Division class is used to hide tags in the code on a separation page or user control code. These classes generated by the designer are stored in a separate file other than the code hidden file.
During the conversion process, Visual Studio 2008 recursively checks every page, master page, and user control in the project, and automatically generates a. designer. cs file for each of them. In addition, Visual Studio changes the. aspx or. ascx file to use the codeBehind attribute instead of the codeFile attribute. This command will also rename the App_Code folder to Old_App_Code.
Convert pages and classes to use branch classes in Web application projects
In Solution Explorer, right-click the project root folder that contains the pages and classes to be converted, and then click convert to Web application ".
Generate a project to check for any compilation errors.
If you see an error, you can check the two most common causes, as described below:
If the Assembly reference is missing, open the reference manager and add it. If you are using a dynamically generated type, see "conversion declarative strong-type dataset" and "conversion configuration file object code" after this topic ".
Because ASP. NET 2.0 dynamically compiles all classes in the App_Code folder, classes compiled as part of the Visual Studio 2008 Web application should not be stored in this folder. Otherwise, the class will be compiled twice. The first compilation will be performed as part of the Visual Studio 2008 Web application project assembly, and the second compilation will be performed by ASP. NET at runtime. This may cause an error in "failed to load type. This exception occurs because duplicate type names exist in the application.
The correct method should be to store the class files in a project folder other than the App_Code folder. This operation is automatically completed by the "convert to Web application" command, which can be renamed to Old_App_Code.
Run a web application project
Now you can compile and run the application. By default, Visual Studio 2008 uses the built-in ASP. NET development server to run the website. You can also configure the project to use Internet Information Service (IIS ). To manage Web Application Project Settings, right-click the project and click Properties ". Then, you can select the "Web" option card to configure these runtime settings.
Other conversion options
When converting a project to a web application project, you also need to consider other conversion options. These options are as follows:
Add the namespace syntax.
Converts declarative and strongly-typed datasets.
Convert the configuration file object code.
Add namespace syntax
By default, pages and classes generated using the Visual Studio 2008 website project model do not automatically contain code namespaces. However, pages, controls, and classes generated using the Visual Studio 2008 web application project model automatically contain code namespaces. When converting a website project to a web application project, you must add a namespace to the code.
For example, when you use C #, you can add a namespace to an existing class in Visual Studio by using the "external code" shortcut menu command in the C # code editor.
Add a namespace to an existing class
Open the code file to be modified.
Select a class (or multiple classes) in the source Editor, right-click the selected code, and click "external code ".
Select the "namespace" item in the list.
The extension is. aspx ,. ascx ,. master ,. ashx ,. asmx and. asax files contain commands with the inherits or class attributes (such as the @ page directive). When these files are code hidden pages, the above attributes list the class names they call. If a namespace is added to a code hidden file of these file types, the namespace must also be added to the inherits and class declarations. For example, if you add a webapplication5 namespace to a code hiding file named details. aspx, you also need to change the inherits attribute of the @ page command from inherits = details_aspx to inherits = webapplication5.details _ Aspx.
Convert declarative and strongly-typed Datasets
If the App_Code folder of the Visual Studio 2008 website project contains a strong dataset class, you must make another change to fix the connectionString element in the Web. config file. In addition, you must set the connectionString element for each TableAdapter object in the dataset class.
Fix connectionstring of a strong Dataset
In the App_Code folder of Solution Explorer, right-click the dataset class and click View designer ".
The designer window for the dataset is displayed.
Right-click the TableAdapter object and click "properties ".
Select "Connection", and then reset the Connection by selecting an available option.
When the connection is updated, the connection string is also updated.
Repeat the preceding steps for each TableAdapter object in each dataset.
Convert the configuration file object code
ASP. NET 2.0 adds support for configuration file attributes. This feature allows you to store and retrieve user configuration file data in a personalized database. In the Visual Studio 2008 website project, ASP. NET automatically adds a Profile object (an instance of the ProfileCommon class) to each ASP. NET webpage in the project ). This object provides strong access to all attributes defined in the profile section of the Application Web. config file. You can obtain the intelliisense data of this object and automatically save and retrieve values in it.
For example, the Web. config file of an application may contain the following parts.
<profile defaultProvider="AspNetSqlProfileProvider"><properties><add name="Teachers"type="Teachers"allowAnonymous="true" /></properties></profile>
In this case, you can add the following code to the 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();}}
Visual Studio 2008 Web application projects do not automatically include the ProfileCommon class. However, you can create your own ProfileCommon class and include the strong type attributes that have been configured in the configuration file system. Then, you can access the current Profile attribute of the HttpContext object to obtain and set these strong attributes. The following example shows how to create a custom ProfileCommon class.
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);}}}}
Next, you can add an instance of the ProfileCommon class named Profile to the page that must use the configuration file system, as shown in the following 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 can use the configuration file system as in the original website project without changing any other code on the page.