Convert a website project to a Web application project in Visual Studio

Source: Internet
Author: User
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:

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

  • . NET Framework 2.0.

    Verify that the project runs correctly before switching the project. This helps prevent errors during conversion.

    Open and verify the Visual Studio 2005 Web project
    1. Open the existing Visual Studio 2005 solution.

    2. Click Open in the File menu, and then click website ".

      The "Open Website" dialog box is displayed.

    3. Select the project folder to open and click open ".

    4. Click Generate website on the generate menu ".

    5. Click Start debugging in the Debug menu ". You can also press F5.

    6. Verify that the project runs as expected.

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
  1. Disable all open solutions in Visual Studio 2008.

  2. Click new in the File menu, and then click Project ".

    The "New Project" dialog box is displayed.

  3. 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.

  4. Select ASP. NET web application ".

  5. Type the values of "name", "location", and "solution name", and click "OK" to create a web application project.

  6. After creating a project, delete the automatically created default. aspx and web. config files.

Copy a website project to a web application project
  1. In Solution Explorer, right-click the website project and select Open folder in Windows Explorer ".

  2. Select the file for which you want to copy the website project.

  3. Right-click the selected file and select copy ".

  4. In the web application project, right-click the web application project and select Open folder in Windows Resource Manager ".

  5. Paste the website project file into the Web application directory.

  6. In Solution Explorer of the Web application project, click show all files.

  7. Select a new file in Solution Explorer.

  8. 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
  1. 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 ".

  2. 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:

  • The Assembly reference that must be added to the project is missing.

  • There is a problem with the dynamically generated type (such as Profile object or typed dataset.

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
  1. Open the code file to be modified.

  2. Select a class (or multiple classes) in the source Editor, right-click the selected code, and click "external code ".

  3. 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
  1. 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.

  2. Right-click the TableAdapter object and click "properties ".

  3. Select "Connection", and then reset the Connection by selecting an available option.

    When the connection is updated, the connection string is also updated.

  4. 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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.