Common Web Project conversion problems and solutions

Source: Internet
Author: User

asp.net and what Visual Studio is.

ASP.net is a technique for creating dynamic Web applications. compiles asp.net pages (Web Forms), so you can generate powerful, form-based Web pages. When you build these pages, you can use the ASP.net user control to create common UI elements and program them for regular tasks.

Visual Studio is an integrated development environment (IDE) that developers can use to create programs for the. NET Framework using one of many programming languages, including C # and Visual Basic.

You will find that converting WEB applications to use the new features of Visual Studio 2005 and ASP.net 2.0 simplifies development and provides more choices for compiling and deploying code.

Web Project Changes

These changes affect how WEB applications are developed, configured, and deployed. As a developer or Web site administrator, you need to understand these changes to correctly build, deploy, and maintain your Web application.

There are no project files. Visual Studio 2005 no longer uses project files to explicitly list files in a Web project. Instead, it takes all files and folders as part of a WEB project. Project information that was previously stored in the project file is now saved in the solution or Web.config file.

Special directory. a asp.net 1.x application has a required folder (/bin) to hold the assembly. The ASP.net 2.0 application has a larger, defined folder structure. The new directory begins with the prefix "App_", which is used to store resources, assemblies, source code, and other components. The new folder structure no longer requires a project file, and some new options are supported at deployment time.

code-behind model. in asp.net 1.x, the code-behind model can isolate content (for example, foo.aspx) from code (for example, Foo.aspx.vb). Content pages inherit from code-behind pages, and code-behind pages contain code generated by the user and designer.
ASP.net 2.0 enhances the code-behind model through partial classes that allow a class to span multiple files. In the new code-behind model, the content page inherits from a compiled class that includes the corresponding code-behind page and an auto-generated partial class that defines the field declarations for the controls used in the content page. This change allows auto-generated code to be detached from user code and makes code-behind pages smaller and simpler. Some class structures also reduce the risk of inadvertently damaging pages by code generated by the editor designer.

Compile the model (one assembly to multiple assemblies). in Visual studio®.net 2003, all code-behind class files and support code are precompiled into a separate assembly with a fixed name. In Visual Studio 2005, create multiple assemblies with a unique generated file name dynamic (default). For example, the default behavior is to compile all the Web forms and user controls in a folder into their own assemblies. The public source code in the App_Code folder is automatically compiled into its own assembly. This new compilation model raises some changes in the Web application architecture, but greatly enhances deployment options and how Web applications work on the Web server.

deployment options (precompiled, fully compiled, updatable sites, and so on). in previous versions of Visual Studio, the Web application was precompiled and deployed as a large assembly. Content pages (for example, *.aspx or *.ascx) are not compiled on the server and cannot be edited. By using the new pages in Visual Studio 2005 to compile the model and folder structure, you can deploy WEB applications in many different configurations. In extreme cases, you can precompile all content pages, their code-behind class files, and their hidden class designer pages, and then deploy a WEB application that contains fully compiled assemblies. In this mode, the application cannot be easily changed on the server. Another extreme scenario is that you can deploy an application that is not precompiled at all. In this configuration, you can change the content page, code-behind class file, or any other code in the application directly on the server. When a user requests a page on a server, the pages are dynamically compiled.

Each of these operational changes may require you to modify the architecture and deployment process of your application before or after you convert the WEB application.

Convert Web Project

Converting a WEB project requires more than just changing the framework version. The conversion consists of three sections:

Pre-convert-view and possibly modify the architecture of your WEB project before running the conversion wizard.

Convert-run the Visual Studio 2005 Transformation Wizard to convert a Web project.

After conversion-resolve any problems that the conversion Wizard did not discover or resolve.

For parts 1th and 2nd, you should read and apply the steps listed in the Step-by-Step guide for converting WEB projects to Visual Studio 2005.

For the 3rd part, you should apply the solutions listed in this paper.

Conversion Wizard

Visual Studio 2005 has a built-in Transformation wizard to help you transform your WEB application. The wizard automates the many basic steps necessary to convert your application to use the ASP.net 2.0 feature.

Run the wizard

The wizard is called automatically whenever you open a Visual Studio. NET 2003 Web project in Visual Studio 2005. The wizard detects whether a Web project file (such as *.vbproj or *.csproj) exists in the application folder, and then automatically starts the conversion process.

Conversion Complete

After the conversion is complete, you should review the conversion report to see if any other manual steps must be performed to complete the conversion.

Conversion Reports

The conversion wizard will record changes to the WEB project in both the XML file and the text file. When the conversion wizard finishes, it displays the XML version of the report. The report will show you all the problems the wizard encounters and the area of your code, and you may need to take additional steps in the code area to complete the wizard.

The report is divided into two parts, one for the transformed solution and the other for the project or projects. There are almost no errors in the solution report. However, the Project section lists multiple issues for each file in the project. You should review this section and resolve any issues reported by the Conversion Wizard.

If you close the conversion report, you may find that there is always a text version at the top of the converted project.

note in the latest version of Visual Studio 2005, the name of the text report is ConversionReport.txt. Later versions will rename this file to Conversionreport.webinfo. A text version can be found in the root folder of the Web application.
Notification Type

Each item in the report falls into one of the following three categories:

Note -notifies you of the action taken by the wizard. You will see a lot of comments about files that have been deleted or moved, as well as deleted or commented out code. Comments are listed only in the text version of the conversion report and are omitted from the XML version.

warning -a warning is generated whenever the wizard has to take actions that might cause behavioral changes or possible compilation errors in your application. A warning is an item you want to view, but you may not need to take action on it.

Error -if the wizard encounters content that cannot be automatically converted, an error item is generated. These items require special processing to complete the conversion. Typically, an error is something that generates a compilation error when you try to run an application. Part 2nd: Common Conversion Issues

Although Visual Studio 2005 is designed to work with code developed with Visual studio®.net 2003, you may experience one or more common conversion problems. In this section, we'll look at some of the most common problems.

The note Conversion Wizard has been upgraded in the latest version of Visual Studio 2005 and can detect and fix some of the following problems. However, if the wizard misses a specific problem, you can manually apply the solution described below to complete the transformation of the WEB project.

problem 1 : code-behind class file (CB-CB) Reference

Note CB is the abbreviation for a Web form (*.aspx) or user control (*.ascx) code-behind file.

The new compilation model uses multiple assemblies that are typically dynamically compiled on the server. This model improves the performance and scalability of the Web site.

However, if your code-behind file references another code-behind file, you will have an interrupted reference because the referenced code-behind file will no longer be in the same assembly.

Here are some common ways you can raise this problem:

Use a Web form or user control as the base class for another Web form or user control.

Use LoadControl () and convert the result to another user control, such as
UserControl1 C1 = (UserControl1) LoadControl ("~/usercontrol1.ascx");

Create a Web forms class instance, such as
WebForm1 W1 = new WebForm1 ();
, where WebForm1 is a class defined in the code-behind file of a Web form.

How to fix

To resolve this issue, you need to change the application to find the reference. Because this is a CB-CB reference, the easiest way to solve the problem is to add a reference instruction to the Web form or user control that made the reference. This tells the compiler which assembly to link to.

We assume that the following conditions are present:

File

asp.net 1.x code

Page1.ascx

-

Page1.ascx.cs

Control1 C = (Control1) LoadControl ("~/control1.ascx");

To change the code to use a reference directive:

File

ASP.net 2.0 Code

Page1.ascx

<%@ Reference control= "~/control1.ascx"%>

Page1.ascx.cs

Control1 C = (Control1) LoadControl ("~/control1.ascx");

By using a reference directive, you can explicitly tell the compiler where to find the Web form or control you want to use. Note that in the latest version of Visual Studio 2005, the Conversion wizard will automate this operation.

problem 2 : Standalone class file (SA-CB) Reference

Note SA is an abbreviation for standalone class files.

If you have a stand-alone class file that references code in a code-behind class file, you may experience another interrupt reference. This is similar to an interrupted CB-CB reference, except that there is a standalone class file in the App_Code folder that attempts to reference a separate page assembly. A common way to raise this problem is to access one of the class variables in the CB class.

How to fix

Fixing a broken SA-CB reference involves more. Because the problem appears in the SA file, the reference directive cannot be used to locate the reference. And after the conversion, the SA file is moved to the App_Code folder, so the class file does not have permission to access the App_Code assembly at compile time.

The solution is to create an abstract base class in the App_Code folder referenced at compile time that will load the actual class from the page assembly at run time.

We assume that the following conditions are present:

File

asp.net 1.x code

Control1.ascx

Inherits= "Control1"

Control1.ascx.cs

Class Control1:System.Web.UI.UserControl {public     static string myname = "Control1";    public void Foo () {Some code}}

Code1.cs

String myname = "Class1 +" + control1.myname;

Change the code to use the abstract base class:

File

ASP.net 2.0 Code

Control1.ascx

Inherits= "Migrated_control1"

Control1.ascx.cs

Class Migrated_control1:control1 {     override public void Foo () {Some Code}}}

App_code/code1.cs

String myname = "Class1 +" + control1.myname;

App_code/stub_control1.cs

Abstract class Control1:System.Web.UI.UserControl {public    static string myname = "Control1";    Abstract public void foo (); }

Because the abstract base class is now in the App_Code folder, allows separate class files and CB files to find a class during compilation (named Control1 in this example). However, the standalone class file will load the original class (in this case, rename to Migrated_control) at run time using the binding. Note: In the latest version of Visual Studio 2005, the Conversion Wizard will automatically create this code.

problem 3 : Circular references

If the code-behind file references other code-behind

File, the referenced code-behind file also references the original file, and a circular application occurs. This situation can occur in two or more code-behind files, such as:

Also occurs between assemblies, where one assembly references another

Assembly, and the referenced assembly also references the original assembly, for example:

How to fix

The first type of circular reference solution is to create an abstract base class in the App_Code folder with one of the references, and then remove the reference directives from the associated Web forms or user control files. This will destroy the circular reference.

The second circular reference is a by-product of the "batch" assembly for performance reasons by the ASP.net compiler. By default, it places Web forms and user controls in a folder and then compiles them into an assembly.

There are many ways to resolve this problem, but we recommend that you move the referenced pages (for example, Pages2.aspx.vb and Pages3.aspx.vb) to their own folders.

By default, the ASP.net compiler creates a separate assembly that contains the pages, and a circular reference between assembly A and B is deleted.

problem 4 : Resource Manager

In Visual Studio. NET 2003, the resource manager is used to manage resources in a WEB application. A typical example looks like this:

File

asp.net 1.x code

Control1.ascx.cs

Assembly a = Assembly.Load ("myApp"); ResourceManager rm = new ResourceManager ("Myapp.resource1", a); String s = rm. GetString ("foo");

Resource1.resx

Contains name/value pair "foo = bar"

This type of code is problematic because it depends on the name of the assembly that you know you want to load, but in Visual Studio 2005 the name is no longer a fixed name.

How to fix

Because Visual Studio 2005 uses an indeterminate assembly name, you need to change the code to use the new resource model. The easiest way to do this is to move the Resource1.resx to a folder named App_GlobalResources. By strong naming, Visual Studio 2005 automatically uses all of the resources found in this folder for Web applications (using IntelliSense to make resources available for discovery). The following is a converted example:

File

ASP.net 2.0 Code

Control1.ascx.cs

String s = Resources.Resource1.foo;

app_globalresources/
Resource1.resx

Resource1.resx moved to App_GlobalResources Contains name/value pair  "foo = bar"

This is just one way to use the new resource model in Visual Studio 2005, and you should review the resource model documentation to discover what's new in the model.

problem 5 : No longer exclude excluded files

In Visual Studio. NET 2003, you must explicitly determine whether files are included in the WEB project. If the containing file is not explicitly listed, the file is excluded from the project. You can also stop building a code file by setting the build operation to none. This information is stored in the project file.

There are several issues to consider when converting, such as:

Files that are excluded from one project may be included in another project

Do you try to convert an excluded file because it might be a piece of code that the user wants to remember?

In the latest version of Visual Studio 2005, the Conversion wizard retains the excluded file as is and is noted in the conversion report. Therefore, your WEB project will contain additional, unsaved files that are now part of the project. Depending on the file's extension, the compiler might attempt to compile the file, which could cause conflicts in the application.

How to fix

After conversion, you can remove any unwanted, previously excluded files from your project. You can also use the Exclude from Project feature, which can be found in Solution Explorer, to rename them by using the secure extension ". Exclude" to effectively remove them from the WEB application.

files excluded by the ". Exclude" extension are still part of the WEB project, but they are not compiled, and Iis/asp.net does not support them if they happen to be on the server. Note The future version of the Conversion wizard will be more proactive and will exclude files if it is considered safe.

problem 6 : Orphaned resx files

Visual Studio. NET 2003 generates a RESX (Resource) file for Web forms and user controls. Typically, users do not use these files because they are automatically generated and may overwrite code that the user has added.

These resx files are not deleted after the conversion because Migration Wizard is not sure whether the user has added the resources that need to be saved.

How to fix

View the generated ResX file and save the user data to its own resource file. It would be better to merge this data into one resource file.

Move the resource file to a special folder App_GlobalResources or App_LocalResources as needed so that the WEB application can use it.

After you save the user data in this manner, you delete the generated RESX files from the Web project.

problem 7 : Additional types in the code-behind file

In Visual studio®.net 2003, you might share types (for example, structs, enumerations, interfaces, modules, and so on) between different pages by storing types in a code-behind file in one of the Web forms or user controls.

The model is interrupted in Visual Studio 2005 because one Web form and multiple user controls are compiled into their own assemblies, and other types are no longer available for discovery.

How to fix

When the conversion wizard finishes the application transformation, any additional non-public types are moved to its own stand-alone code file in the App_Code folder. Because you must work across assemblies, you may also need to change the access modifier to public. With WEB applications, shared types are automatically compiled and are available for discovery.

problem 8 : Access to automatically generated control variables

In Visual studio®.net 2003, code-behind class files contain user and auto-generated designer code. The latter can contain control variable declarations and page functionality. While this is not recommended, some developers change the access rights of control variables to public so that they can be modified outside their classes. Examples are shown below:

File

asp.net 1.x code

Userctrl1.ascx

UC1

UserCtrl1.ascx.cs

Public System.Web.UI.WebControls.Label Label1;

Page1.ascx.cs

USERCTRL1 uc1 = (USERCTRL1) LoadControl ("~/userctrl1.ascx"); Label1.Text = "Foo";

In Visual Studio 2005, this change does not work because the user is separated from the auto-generated designer code by a partial class. Some classes allow a class to span multiple files and are used to maintain a clean separation between users and auto-generated code.

How to fix

In general, it is recommended that you change the code to an access level that does not depend on changes to the auto-generated code. However, if there are hundreds of locations in your code that invoke variables such as automatically generating such a variable and need to run the code quickly, then there is a useful alternative. You can rename the original control variable to a different name, and then create a public property to access the renamed control variable. For example:

File

ASP.net 2.0 Code

Userctrl1.ascx

UC1

USERCTRL1 Hidden Partial class

This is auto-generated in the hidden, partial classprotected System.Web.UI.WebControls.Label p_label1;

UserCtrl1.ascx.cs

Public System.Web.UI.WebControls.Label Label1 {get    {return p_label1;}    set {P_label1 = value;}}

Page1.ascx.cs

USERCTRL1 uc1 = (USERCTRL1) LoadControl ("~/userctrl1.ascx"); Label1.Text = "Foo";

problem 9 : Unable to switch to Design view

The new Visual Web Designer built into Visual Studio 2005 is more restrictive than Visual Studio. NET 2003 in the correct HTML. If the ASPX page contains mismatched tags or malformed HTML, the designer will not allow you to switch to Design view within Visual Studio 2005. Instead, you will be limited to code view until the problem is fixed. This problem occurs because Visual Studio 2005 has a new source code retention and validation feature built into it.

How to fix

To avoid this problem, all you have to do is make sure that the tags in the ASPX page are formatted correctly. If you encounter problems when switching from Code view to Design view, the problem is almost certainly the wrong sign.

problem Unable to resolve file name

You may see some vague error messages telling you that you cannot parse a file. This means that the ' codebehind ' or ' SRC ' attribute cannot be found on the HTML tag.

If your Web form or user control does not contain any of these properties, the wizard cannot find a matching code-behind file or convert the page.

How to fix

If this happens to be a plain HTML page, the error can be ignored and frequently encountered if the application has an HTML page that uses an ASPX extension.

To avoid this problem, make sure that you name the HTML file correctly and use the ' codebehind ' and ' Src ' attributes in the Web forms and user controls on the HTML markup.

note in the next version of the Web Project Migration Wizard, you can make this error easier to describe by using the following message.

For more information, see: http://msdn2.microsoft.com/zh-cn/library/aa479312.aspx


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.