To convert a Web site project to a Web form application

Source: Internet
Author: User

Transfer from http://blog.sina.com.cn/s/blog_53729e4601014ze9.html

This article describes 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 the Web project in Visual Studio. NET 2003. The conceptual approach includes a project file that contains files and compilations into a single assembly, or excludes them.

To complete the conversion operation, take the following steps:

    • Open and verify the Visual Studio 2005 Web site project.

    • Create a new Visual Studio WEB application project.

    • Sets the project reference.

    • Copy the files to the new Web application project.

    • Convert the project file.

    • Run the WEB application project.

    • Adds a namespace syntax.

    • Converts a declarative strongly typed DataSet.

    • Transforms the profile object code.

To complete the conversion, you need to:

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

    • . NET Framework version 2.0.

      Before you convert a project, you should verify that the project runs correctly. This helps prevent errors from occurring during conversion.

      Open and verify the Visual Studio 2005 WEB Project
      1. Open an existing Visual Studio 2005 solution.

      2. On 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. On the Build menu, click Build Web site.

      5. On the Debug menu, click Start Debugging. You can also press F5.

      6. Verify that the project is running as expected.


To create a new Visual Studio WEB Application project

The best strategy for converting an existing Visual Studio 2005 Web site project is to create a new, blank Visual Studio Web application project in a separate directory. This avoids changes to any part of the existing Web site file. It also facilitates the copying of existing functionality into a new WEB application project.

You can add a new project to an existing solution, which is the ideal scenario when there are multiple class library projects 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 WEB application project in a new solution
    1. In Visual Studio 2008, close all open solutions.

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

      The New Project dialog box is displayed.

    3. In the New Project dialog box, in the Project Types section, 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 a value for name, location, and solution name, and then click OK to create the WEB application project.

    6. After you create the project, delete the automatically created Default.aspx and Web. config files.



To copy a Web site project to a Web application project
    1. In Solution Explorer, right-click the Web site project, and then select Open Folder in Windows Explorer.

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

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

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

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

    6. In Solution Explorer for the WEB application project, click the Show All Files button.

    7. In Solution Explorer, select the new file.

    8. Right-click the selected file, and then select Include in Project.

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

After you copy a file from a Web site project to a Web application project, you find that the code-behind file for each page and user control is still associated with the. aspx,. master, and. ascx files. This procedure does not generate a. designer.cs or. designer.vb file. In the next step, you will convert these pages so that their partial classes are saved in a. designer.cs or. designer.vb file.

Converting project files

Visual Studio includes an option for converting pages and classes in a WEB application project to use their partial classes. Partial classes are used to delimit markup in a page or user control code-behind code. These designer-generated classes are saved in a separate file from the code-behind file.

During the conversion, Visual Studio 2008 recursively examines each page, master page, and user control in the project and automatically generates a. designer.cs file for each item in them. In addition, Visual Studio changes the. aspx or. ascx file to use the Codebehind property instead of the CodeFile property. 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 project root folder that contains the pages and classes that you want to convert, and then select Convert to Web application.

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

If you see an error, you can check the two most common causes, as described below:

    • The assembly references that must be added to the project are missing.

    • There is a problem with dynamically generated types, such as profile objects or typed datasets.

If an assembly reference is missing, open the reference manager and add it. If you are using dynamically generated types, see "Converting declarative strongly typed datasets" and "Transforming 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 the classes that are compiled as part of the Visual Studio Web application in that folder. Otherwise, the class will be compiled two times. The first time is compiled as part of the Visual Studio WEB Application project assembly, and the second is compiled at run time by ASP. This may cause the "Failed to load Type" exception. This exception occurs because there is a duplicate type name in the application.

The correct approach should be to store the class file in a project folder 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.

Run the WEB Application project

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




Other Conversion options

When you convert your project to a WEB application project, you also need to consider some other conversion options. These options are as follows:

    • Adds a namespace syntax.

    • Converts a declarative strongly typed DataSet.

    • Transforms the profile object code.

Add namespace syntax

By default, pages and classes that are generated by using the Visual Studio 2008 Web site project Model do not automatically include the code namespace. However, pages, controls, and classes that are generated by using the Visual Studio WEB Application project model automatically include the code namespace. When you convert a Web site project to a Web application project, you must add a namespace to your code.

For example, when you use C #, you can add namespaces to existing classes in Visual Studio through the External Code shortcut menu command in the C # code Editor.

To add a namespace to an existing class
    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 outside code.

    3. Select the namespace item in the list.

Files with the extension. aspx,. ascx,. master,. ashx,. asmx, and. asax contain directives with Inherits or class attributes, such as the @ Page directive, which lists the class names they call when the files are code-behind pages. If you add a namespace to a code-behind file that belongs to these file types, you must also add the namespace to the Inherits and Class declarations. For example, if you add a WebApplication5 namespace to a code-behind file named Details.aspx, you also need to change the Inherits property of the @ Page directive from inherits=details_aspx to inherits= Webapplication5.details_aspx.



Converting a declarative strongly typed dataset

If the App_Code folder of a Visual Studio 2008 Web site project contains a strongly typed dataset class, you must also perform another change to repair 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 for strongly typed datasets
    1. In Solution Explorer, in the App_Code folder, right-click the DataSet class, and then click View Designer.

      The designer window for the dataset is displayed.

    2. Right-click the TableAdapter object, and then select Properties.

    3. Select the Connection property, and then reset the connection by selecting one of the available options.

      When the connection is updated, the connection string is updated as well.

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


Convert profile object code

ASP. NET 2.0 adds support for profile properties. With this feature, user profile data can be stored and retrieved in a personalized database. In a Visual Studio 2008 Web site project, ASP. NET automatically adds a profile object (an instance of the ProfileCommon Class) to each ASP. This object provides strongly-typed access to all of the properties defined in the profile section of the application Web. config file. You can get IntelliSense data for this object and automatically save and retrieve values in it.

For example, the Web. config file for an application may contain the following sections.

<profile defaultprovider= "AspNetSqlProfileProvider" > <properties> <add name= "Teachers" type= " Teachers "allowanonymous=" true "/> </properties> </profile>
PostScript open an ASP. When you try to edit a content page in Design view, you don't see anything, you can't make visual edits, and you can't create an event handler in a code file. It is very confusing to meet this situation. So the idea of converting an ASP. asp. NET Web site to a project can solve this problem, and the result is yes.

To convert a Web site project to a Web Form application (GO)

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.