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
Open an existing Visual Studio 2005 solution.
On the File menu, click Open, and then click Web Site. The Open Web Site dialog box is displayed.
Select the project folder that you want to open, and then click Open.
On the Build menu, click Build Web site.
On the Debug menu, click Start Debugging. You can also press F5.
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
In Visual Studio 2008, close all open solutions.
On the File menu, click New, and then click Project. The New Project dialog box is displayed.
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.
Select "ASP. NET Web application".
Type a value for name, location, and solution name, and then click OK to create the WEB application project.
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
In Solution Explorer, right-click the Web site project, and then select Open Folder in Windows Explorer.
Select the file for which you want to copy the Web site project.
Right-click the selected file and select Copy.
In the Web application project, right-click the Web Application project, and then select Open Folder in Windows Explorer.
Paste the Web site project file into the Web application directory.
In Solution Explorer for the WEB application project, click the Show All Files button.
In Solution Explorer, select the new file.
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 filesVisual 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
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.
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 projectYou 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:
Add namespace syntaxBy 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
Open the code file that you want to modify.
Select a class (or multiple classes) in the source editor, right-click the selected code, and then click outside code.
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 datasetIf 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
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.
Right-click the TableAdapter object, and then select Properties.
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.
Repeat these steps for each TableAdapter object in the dataset.
Convert profile object codeASP. 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. |