Microsoft.NET

……………………………………………….Expertise in .NET Technologies

ASP.NET2.0 Migration Overview

Posted by Ravi Varma Thumati on April 24, 2009

ASP.NET 2.0

The latest version of ASP.NET, 2.0, is more than an upgrade—it is a major update of the technology, as it changes the way ASP.NET applications are designed, compiled, and deployed. Microsoft does recognize the existence of a large number of ASP.NET 1.1 installations, so 2.0 was designed to allow older applications to work without problems. On the other hand, the need to convert existing applications to the 2.0 platform may arise. With that said, let’s examine the major differences between the versions, as well as areas that may be problems during the conversion process.

Version changes

While there are many changes between ASP.NET 1.1 and 2.0, there are certain ones that have a greater impact of project conversion. The following list provides an overview of the key issues:

Code-behind model: ASP.NET 2.0 introduces the concept of partial classes, whereas a class can span multiple class files. It is a key feature used in Visual Studio 2005 to separate system-generated code from user code. This negates the scenario where you find yourself scrolling through source code and encountering the code generated by the system. The partial class approach reduces the risk of inadvertently editing system code and results in smaller code-behind files.

Directories: ASP.NET 2.0 has added a number of special directories where 1.1 had only the one required bin directory. Most of these new directories have the App_ prefix with names like App_Code, App_Data, App_GlobalResources, and App_LocalResources, while the bin directory still exists along with a themes directory. The new directories are a key aspect of 2.0’s elimination of project files.

No more project files: Gone are the days of project files with ASP.NET and Visual Studio 2005. The project is now the complete Web project directory, which is a drastic shift from 1.1 and can cause migration issues as described later.

Compilation model: With 1.1, all code was compiled into one assembly placed in the bin directory. With 2.0, the assembly is separated into multiple assemblies. These multiple assemblies may be created on-the-fly or precompiled. Examples of multiple assemblies are one assembly for each ASP.NET directory like App_Code and App_Data as well as individual assemblies for Web Forms, User Controls, and so forth. This is a major shift in the application structure; it offers more deployment options in how the application is delivered to the users.

Application deployment: The deployment of 1.1 applications was fairly straightforward as you moved the content files along with the necessary assembly files—it could easily be accomplished with a simple copy and paste with limited options. The 2.0 model provides various options for deploying an application. For instance, you may choose to pre-compile all code and deploy it or go the other way with no pre-compilation. Also, you can lock down deployed content files so no changes may be made after deployment (this is a major difference from 1.1).

The list provides a high-level view of the main differences between ASP.NET 1.1 and 2.0 and what can affect the conversion of an existing application to 2.0. Now let’s take a closer look at possible problem areas.

Application conversion

The major differences covered in the previous section provide clues of where you may experience problems when converting an application. The following list highlights areas where you need to be careful when moving an application to the new ASP.NET 2.0 platform:

You should be certain the application compiles with no problems with 1.1 before starting the conversion process. With a fully functional project, make a backup and then start the conversion.

2.0’s obliteration of the project file is a drastic shift from 1.1. With 2.0, all references have been moved to their own directories. In addition, the Web project folder is now the project so older 1.1 projects with multiple sites will need to be split into separate projects with 2.0. You’ll need to separate the projects before converting them.

The new directory structure in 2.0 requires resources and other components used in 1.1 that you will need to shift to the new directories.

The new compilation structure with multiple assemblies generated will likely change the way the application is deployed, so be aware of the different assemblies when deploying the converted application.

The .NET Framework has changed from 1.1 to 2.0, so there may be features of 1.1 that are no longer supported in 2.0 (like classes or their methods and/or properties).

Follow the wizard

Microsoft does not leave you in the cold when approaching a conversion project. Visual Studio 2005 includes a conversion wizard to walk you through the conversion of a project.

The Visual Studio Conversion Wizard begins by offering to make a backup of the existing application before proceeding (which is a very good idea). Next, it proceeds with conversion and presents a log of the process when it has completed. Common issues that may arise involve features of the .NET Framework that may have been replaced or removed in the latest version. Listing A is an example of one of these issues.

Listing A

System.Collections.Specialized.NameValueCollection’ is obsolete:

 ‘This method is obsolete, it has been replaced by

System.Configuration!System.Configuration.ConfigurationManager.AppSettings’

This message in Listing A tells us a feature has been replaced by another feature in the latest platform version, so we must manually make the change to the code. Depending upon the application, there may be numerous issues along these lines. You’ll need to address each issue individually and make the necessary changes to fully take advantage of the 2.0 platform.

ASP.NET2.0 Migration Overview 

Many of the programming concepts used in previous versions of ASP.NET remain the same in ASP.NET version 2.0, so developing applications in ASP.NET 2.0 will be familiar for users of ASP.NET 1.x. ASP.NET 2.0 will run on the same operating systems and hardware that you use to run your ASP.NET 1.x applications, including Microsoft Windows 2000 and Microsoft Internet Information Services (IIS) 5.0, Microsoft Windows XP and IIS 5.1, and Microsoft Windows Server 2003 and IIS 6.0.

If you have an existing Web application that you have decided to migrate to ASP.NET, you should review the new features of ASP.NET 2.0 before migrating. The most important changes involve the page code-behind model, the Web application folder structure, and the page compilation model.

Before migrating you should ensure that your ASP.NET 1.x application can be compiled and run on the .NET Framework version it was developed on. Additionally, before starting migration, ensure that the Microsoft .NET Framework version 2.0 is installed and working.

There are many general considerations that should be taken into account before migrating. The following sections are discussed in this topic:

  • Benefits to Migrating
  • Page Models
  • Sharing Data Across ASP.NET Versions
  • Forms Authentication Across ASP.NET Versions
  • Name Conflicts
  • Markup Compliance
  • HttpOnly and Cross-Site Scripting

Benefits to Migrating

There are many benefits to migrating your Web application to ASP.NET 2.0, including improved markup and code separation, reserved application folders, and automatic code compilation.

The new code-behind model based on partial classes allows greater separation of markup and code, and removes the need for control declarations and event wire-up code in your code-behind files. In the ASP.NET page compilation model, code-behind files are compiled at run time into multiple assemblies on first request to the corresponding .aspx files.

 ASP.NET now uses an improved Web application structure based on reserved folders. These folders contain specific content to help you organize your application more efficiently. Some reserved folders, like App_Data, do not serve their content in response to Web requests but can be accessed from application code.

 The ASP.NET 2.0 compiler automatically compiles application code and dependent resources when a request is made to a resource on your Web site. For example, changes to an existing Web page or dependent resources in ASP.NET 2.0 can simply be saved and the page requested again for the page and its resources to be recompiled. This applies to resources like code files in the App_Code folder, resource files in the App_GlobalResources and App_LocalResources folders, and themes in the App_Themes folder

If you are migrating a large application, it is recommended you use Visual Web Developer 2005, Visual Web Developer 2005 Express Edition, Visual Studio 2005, or Visual Studio 2005 Team System, each of which has a migration wizard that automates many of the tasks involved in a typical migration. The wizard makes the necessary changes to convert ASP.NET 1.x Web applications to ASP.NET 2.0.

It is not necessary to migrate Web applications because ASP.NET 2.0 provides a high degree of backward compatibility with previous versions. If you do not migrate, you can still use many of the features of ASP.NET 2.0 in your Web application, as long as your application uses the .NET Framework 2.0. You only need to configure your existing Web application to use the .NET Framework 2.0.

Page Models

The ASP.NET code-behind Web page model allows you to store the markup of a Web page in one file – the .aspx file – and the programming code in another file – the code-behind file. The code-behind model for ASP.NET 2.0 is different than previous versions in that it uses a new language feature known as partial classes. The new code-behind model in ASP.NET 2.0 allows even greater separation of markup and code than previous versions of ASP.NET.

Web pages that use the old code-behind model based on the CodeBehind attribute of the @Page directive will continue to work in ASP.NET 2.0. However, it is recommended that you migrate these pages to the new code-behind model using the CodeFile attribute of the @Page directive and a partial class definition in the code-behind file to take advantage of improved markup and code separation as well as automatic page compilation. Web pages that use the old code-behind model must be manually compiled.

You can make the changes manually, or you can use one of the Microsoft Visual Studio 2005 editions that includes a migration wizard, such as Visual Web Developer 2005 Express Edition.

ASP.NET version 1.1 also supports a variant of the code-behind model in which the CodeBehind attribute of the @Page directive was replaced with the Src attribute. A Web page using the Src attribute will continue to work in ASP.NET 2.0 and does not require modification to run.

As an alternate to the code-behind model, the single-file page model places the page’s markup and programming code in the same physical .aspx file. Single-file pages from previous versions of ASP.NET will continue to work in ASP.NET 2.0 and do not require modification to run.

Sharing Data across ASP.NET Versions

You can choose to migrate some parts of your Web site to ASP.NET 2.0 while leaving others unmodified. If your Web site is divided into independent Web applications working together to provide your Web site functionality, you might choose to migrate some applications and not others. In this scenario, you will not be able to share application state between migrated applications and non-migrated applications. Session state will likewise not be shared across applications unless you provide a custom session state solution that can be accessed from both ASP.NET 1.x and ASP.NET 2.0 pages.

Forms Authentication across ASP.NET Versions

Forms authentication provides you with a way to authenticate users using your own code and then maintain an authentication token in a cookie or in the page URL. ASP.NET forms authentication can be made to work across applications running different ASP.NET versions so that authentication tickets issued by one version can be consumed by the other.

Configuring authentication to work across ASP.NET versions is similar to the process of configuring authentication across a network of Web servers (a Web farm). In both cases, you explicitly set the validationKey and decryptionKey attributes of the machineKey element for the applications sharing authentication tickets with the same key. To support authentication across ASP.NET versions, you must make the additional configuration change of setting the decryption attribute of the machineKey element to 3DES in your ASP.NET 2.0 application’s Web.config file. The default encryption for ASP.NET 2.0 is AES, whereas previous versions of ASP.NET use 3DES.

Name Conflicts

Before migrating, it is recommended that you scan your Web application and look for names that conflict with feature classes and namespaces in the .NET Framework 2.0. Conflicts might arise when common names like cache, membership, profile, and role are used in your Web application but are already in use in the .NET Framework. To avoid a naming conflict, locate references in your code to any names in question and use a fully qualified reference.

ASP.NET 2.0 uses a different Web site layout than previous versions. To make it easier to work with Web applications, ASP.NET reserves certain folder and file names that you can use for specific types of content. Content in reserved folders is not served to Web requests made to it; this can lead to issues in your existing application. Therefore, before migrating individual application files it is recommended you change the names of any of your application folders or files that conflict with ASP.NET 2.0 reserved folder and file names.

Markup Compliance

By default, all markup that is produced by ASP.NET and the Web server controls included with ASP.NET now conforms to the XHTML 1.0 Transitional standard. This can lead to unintended HTML rendering issues after migration. To help in migration of a Web application, you can set the mode attribute of the xhtmlConformance element to Legacy in your Web.config file. This should be a temporary step in the migration process. In the long term, it is recommended you run your application with the mode attribute of the xhtmlConformance element set to Transitional. Additionally, it is recommended that you add the DOCTYPE element to your migrated pages.

HttpOnly and Cross-Site Scripting

The HttpOnly property of the HttpCookie class is new in the .NET Framework 2.0. Setting this property to true can help mitigate cross-site scripting threats. The HttpOnly property is set to true automatically for forms authentication cookies and session ID cookies so that these cookies are not available to client-side scripting. If a migrated Web page throws a NullReferenceException exception, it might indicate that the session from the client was lost because the HttpOnly property was set to true. If this is the case, you can use one of the following resolutions:

  • Set the HttpOnly property of each cookie to false in the EndRequest event handler of the HttpApplication class in the Global.asax file.
  • Write a custom module that copies the cookie into another cookie, and clears the HttpOnly property so it can be manipulated by client script.
  • Write a custom session ID manager that does not set the HttpOnly property to true

 

How to: Migrate an ASP.NET 1.1 Web Page Using the Codebehind Attribute to ASP.NET 2.0 

This topic demonstrates how to create a Web page and user control that use the Codebehind attribute of the @Page directive, compile them, and then convert them to use the CodeFile attribute with a partial class in the code-behind files in the .NET Framework version 2.0.

The new Web page code-behind model in ASP.NET version 2.0 is based on partial classes. The markup for the page is stored in one file – the .aspx file – and the code is defined in a partial class – the code-behind file. Converting your existing Web pages to the new code-behind model allows even greater separation of markup and code because you do not have to include instance variables or explicit event binding in the partial class.

Web pages using the Codebehind and Inherits attributes of the @Page directive will continue to work in the .NET Framework 2.0. However, you must compile code-behind files and put the resulting assembly in the Bin folder.

If you choose to migrate to the ASP.NET 2.0 code-behind model, you must make coordinated changes to your .aspx file and code-behind file. In your .aspx file, you will replace the Codebehind attribute with the CodeFile attribute. In your code-behind files, you will use a partial class. The advantage of using the new code-behind model is that you do not have to explicitly compile the code-behind files because the ASP.NET compiler will do this automatically.

Additionally, when using the new code-behind model, references to other code-behind files must be added using @Register directives.

To access the Web page created in the procedures below, you will need to create a virtual directory in Microsoft Internet Information Services (IIS).

To compile a Web page and user control using the CodeBehind attribute

1.           Create a Web page using the Codebehind attribute of the @Page directive, as shown in the following code example.

Visual Basic

<%@ Page Language=”VB” AutoEventWireup=”true” CodeBehind=”CodeBehindExample.aspx.vb” Inherits=”CodeBehindExample” %> <!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN” “http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd”&gt; <html > <head runat=”server”> <title>Code-Behind Using the CodeBehind Attribute</title> </head> <body> <form id=”form1″ runat=”server”> <div> <asp:Label id=”Label1″ runat=”server”></asp:Label> </div> </form> </body> </html>

C#

<%@ Page Language=”C#” AutoEventWireup=”true” CodeBehind=”CodeBehindExample.aspx.cs” Inherits=”CodeBehindExample” %> <!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN” “http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd”&gt; <html > <head runat=”server”> <title>Code-Behind Example</title> </head> <body> <form id=”form1″ runat=”server”> <div> <asp:Label id=”Label1″ runat=”server”></asp:Label> </div> </form> </body> </html>

Visual Basic

Public Class CodeBehindExample Inherits System.Web.UI.Page Protected Label1 As System.Web.UI.WebControls.Label Protected Sub Page_Load(ByVal sender As Object, _ ByVal e As System.EventArgs) Handles Me.Load Dim uc As CodeBehindExampleUserControl = _ CType(LoadControl(“~/CodeBehindExampleUserControl.ascx”), _ CodeBehindExampleUserControl) Label1.Text = CType(uc.FindControl(“Label2”), _ System.Web.UI.WebControls.Label).Text End Sub End Class

C#

public class CodeBehindExample : System.Web.UI.Page { protected System.Web.UI.WebControls.Label Label1; protected void Page_Load(object sender, System.EventArgs e) { CodeBehindExampleUserControl uc = (CodeBehindExampleUserControl)LoadControl (“~/CodeBehindExampleUserControl.ascx”); Label1.Text = ((System.Web.UI.WebControls.Label) uc.FindControl(“Label2”)).Text; } }

Note that for the Label control declared on the .aspx page, there is a corresponding declaration in the code-behind file. Also, the code-behind class is a normal class definition and not a partial class.

2.           Create a user control using the Codebehind attribute of the @Control directive, as shown in the following code example.

Visual Basic

<%@ Control Language=”VB” AutoEventWireup=”false” CodeBehind=”CodeBehindExampleUserControl.ascx.vb” Inherits=”CodeBehindExampleUserControl” %> <asp:Label id=”Label2″ runat=”server”> Label text in user control. </asp:Label>

C#

<%@ Control Language=”C#” AutoEventWireup=”false” CodeBehind=”CodeBehindExampleUserControl.ascx.cs” Inherits=”CodeBehindExampleUserControl” %> <asp:Label id=”Label2″ runat=”server”> Label text in user control. </asp:Label>

Visual Basic

Public Class CodeBehindExampleUserControl Inherits System.Web.UI.UserControl Protected Sub Page_Load(ByVal sender As Object, _ ByVal e As System.EventArgs) Handles Me.Load ‘ User control code. End Sub End Class

C#

public class CodeBehindExampleUserControl : System.Web.UI.UserControl { protected void Page_Load(object sender, System.EventArgs e) { // User control code. } }

Note that, similar to the code-behind file for the Web page, the code-behind file for the user control uses a class and not a partial class.

3.           Compile the Web page and user control, and place the code library into the Bin folder of your application. Using the Codebehind attribute requires you to compile all code-behind classes into a single code library and place the resulting .dll file in the Bin folder. For the Web page and user control example, the compile statement that compiles both code-behind files is:

Visual Basic

vbc /target:library /nologo /out:bin\CodeBehindExample.dll /r:System.dll,System.Web.dll CodeBehindExample.aspx.vb CodeBehindExampleUserControl.ascx.vb

C#

csc /target:library /nologo /out:bin\CodeBehindExample.dll CodeBehindExample.aspx.cs CodeBehindExampleUserControl.ascx.cs

4.           Request the Web page CodeBehindExample.aspx in a browser to verify that it works in the .NET Framework 2.0.

To convert a Web page and user control using the CodeBehind attribute to the ASP.NET 2.0 code-behind model

1.           Make changes to the Web page’s .aspx file to match the following code example.

Visual Basic

<%@ Reference Control=”~/CodeBehindExampleUserControl.ascx” %> <%@ Page Language=”VB” AutoEventWireup=”false” CodeFile=”CodeBehindExample.aspx.vb” Inherits=”CodeBehindExample” %> <!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN” “http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd”&gt; <html > <head runat=”server”> <title>Code-Behind Example</title> </head> <body> <form id=”form1″ runat=”server”> <div> <asp:Label id=”Label1″ runat=”server”></asp:Label> </div> </form> </body> </html>

C#

<%@ Reference Control=”~/CodeBehindExampleUserControl.ascx” %> <%@ Page Language=”C#” AutoEventWireup=”true” CodeFile=”CodeBehindExample.aspx.cs” Inherits=”CodeBehindExample” %> <!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN” “http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd”&gt; <html > <head runat=”server”> <title>Code-Behind Example</title> </head> <body> <form id=”form1″ runat=”server”> <div> <asp:Label id=”Label1″ runat=”server”></asp:Label> </div> </form> </body> </html>

A CodeFile attribute is now used instead of a Codebehind attribute in the @Page directive of the .aspx file. A @Reference directive is used to reference the user control in the code-behind page.

2.           Make changes to your Web pages code-behind file to match the following code example.

Visual Basic

Partial Class CodeBehindExample Inherits System.Web.UI.Page Protected Sub Page_Load(ByVal sender As Object, _ ByVal e As System.EventArgs) Handles Me.Load Dim uc As CodeBehindExampleUserControl = _ CType(LoadControl(“~/CodeBehindExampleUserControl.ascx”), _ CodeBehindExampleUserControl) Label1.Text = CType(uc.FindControl(“Label2”), _ System.Web.UI.WebControls.Label).Text End Sub End Class

C#

public partial class CodeBehindExample : System.Web.UI.Page { protected void Page_Load(object sender, System.EventArgs e) { CodeBehindExampleUserControl uc = (CodeBehindExampleUserControl)LoadControl (“~/CodeBehindExampleUserControl.ascx”); Label1.Text = ((System.Web.UI.WebControls.Label) uc.FindControl(“Label2”)).Text; } }

In the code-behind file, a partial class is used to define additional coding for the .aspx page. There is now no need to declare the Label control used on the .aspx page, as was the case with the previous code-behind page based on the Codebehind attribute.

3.           Make changes to your user control’s .aspx file to match the following code example.

Visual Basic

<%@ Control Language=”VB” AutoEventWireup=”false” CodeFile=”CodeBehindExampleUserControl.ascx.vb” Inherits=”CodeBehindExampleUserControl” %> <asp:Label id=”Label2″ runat=”server”> Label text in user control. </asp:Label>

C#

<%@ Control Language=”C#” AutoEventWireup=”true” CodeFile=”CodeBehindExampleUserControl.ascx.cs” Inherits=”CodeBehindExampleUserControl” %> <asp:Label id=”Label2″ runat=”server”> Label text in user control. </asp:Label>

Similar to the Web page, the user control now uses a CodeFile attribute instead of the Codebehind attribute.

4.           Make changes to your user control’s code-behind file to match the following code example.

Visual Basic

Partial Class CodeBehindExampleUserControl Inherits System.Web.UI.UserControl Protected Sub Page_Load(ByVal sender As Object, _ ByVal e As System.EventArgs) Handles Me.Load ‘ User control code. End Sub End Class

C#

public partial class CodeBehindExampleUserControl : System.Web.UI.UserControl { protected void Page_Load(object sender, System.EventArgs e) { // User control code. } }

Similar to the Web page’s code-behind file, the user control’s code-behind file now uses a partial class.

5.           Delete the CodeBehindExample.dll file from the Bin folder that was created in the previous procedure.

ASP.NET 2.0 will compile your page on first request along with its code-behind files.

6.           Request the Web page CodeBehindExample.aspx and verify that it works as before.

 

How to: Migrate an ASP.NET 1.x Web Page to Use Strongly Typed Resources 

This topic demonstrates how to migrate an ASP.NET version 1.x Web page that uses resources to an ASP.NET version 2.0 Web page that uses strongly typed resources.

In ASP.NET 2.0 you can create strongly typed resources by putting the resources in the App_GlobalResources folder of you application. A strongly typed resource can be programmatically accessed through the System.Resources namespace. A strongly typed resource can also be accessed declaratively by using a resource expression. Both methods for adding resources to the Web page are shown in the procedure below.

The procedures below require you to use the .NET Framework version 2.0.

To use a resource from an assembly on a Web page

1.           Create a resource file named Resource1.resx that has one string resource named String1 with a value of String from resource.

 

2.           Convert the Resource1.resx file into a binary resource file (.resource) using the Resource File Generator (Resgen.exe)  with the following command.

resgen Resource1.resx Resource1.resources

3.           Compile the Resource.resource file into an assembly named Resource1Assembly.dll using the following command.

Visual Basic

vbc /t:library /out:bin\Resource1Assembly.dll /res:Resource1.resources

C#

csc /t:library /out:bin\Resource1Assembly.dll /res:Resource1.resources

4.           Create a Web page that uses the resource, as shown in the following code example.

Visual Basic

<%@ Page Language=”VB” %> <%@ Import Namespace=”System.Reflection” %> <%@ Import Namespace=”System.Resources” %> <!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN” “http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd”&gt; <script runat=”server”> Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Dim rm As New ResourceManager(“Resource1”, _ Assembly.Load(“Resource1Assembly”)) Label1.Text = rm.GetString(“String1″) End Sub </script> <html > <head runat=”server”> <title>Resource Example</title> </head> <body> <form id=”form1″ runat=”server”> <div> <asp:Label id=”Label1″ runat=”server”/> </div> </form> </body> </html>

C#

<%@ Page Language=”C#” %> <%@ Import Namespace=”System.Reflection” %> <%@ Import Namespace=”System.Resources” %> <!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN” “http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd”&gt; <script runat=”server”> protected void Page_Load(object sender, EventArgs e) { ResourceManager rm = new ResourceManager(“Resource1”, Assembly.Load(“Resource1Assembly”)); Label1.Text = rm.GetString(“String1″); } </script> <html > <head runat=”server”> <title>Resource Example</title> </head> <body> <form id=”form1″ runat=”server”> <div> <asp:Label id=”Label1″ runat=”server”/> </div> </form> </body> </html>

5.           Request the Web page in a browser to verify that it works in the .NET Framework 2.0.

The Label control text is set programmatically using the resource string. In this example, the ResourceManager class reads the resource from an assembly.

To use a resource in the App_GlobalResoures folder

1.           Using the Resource1.resx file from the previous procedure, add two more string resources: String2 with a value of Second string from resource and String3 with a value of Third string from resource.

2.           Create a folder in the root of your Web application titled App_GlobalResources, if one does not already exist. Put the Resource1.resx file in the App_GlobalResources folder.

3.           Create a Web page that uses the resource, as shown in the following code example.

Visual Basic

<%@ Page Language=”VB” %> <!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN” “http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd”&gt; <script runat=”server”> Protected Sub Page_Load(ByVal sender As Object, _ ByVal e As System.EventArgs) Label1.Text = Resources.Resource1.String1 Label2.Text = CType(GetGlobalResourceObject(“Resource1”, _ “String2″), String) End Sub </script> <html > <head runat=”server”> <title>Resource Example</title> </head> <body> <form id=”form1″ runat=”server”> <div> <asp:Label id=”Label1″ runat=”server”/> <br /> <asp:Label id=”Label2″ runat=”server”/> <br /> <asp:Label id=”Label3″ Text=”<%$ Resources: Resource1, String3 %>” runat=”server” /> </div> </form> </body> </html>

C#

<%@ Page Language=”C#” %> <!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN” “http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd”&gt; <script runat=”server”> protected void Page_Load(object sender, EventArgs e) { Label1.Text = Resources.Resource1.String1; Label2.Text = (String)GetGlobalResourceObject(“Resource1”, “String2″); } </script> <html > <head runat=”server”> <title>Resource Example</title> </head> <body> <form id=”form1″ runat=”server”> <div> <asp:Label id=”Label1″ runat=”server”/> <br /> <asp:Label id=”Label2″ runat=”server”/> <br /> <asp:Label id=”Label3″ Text=”<%$ Resources: Resource1, String3 %>” runat=”server” /> </div> </form> </body> </html>

4.           Request the Web page in a browser to verify that it works in the .NET Framework 2.0.

The three Label controls on the Web page are set to the three resource strings in the Resource1.resx file. The Label1 control is set programmatically by accessing the compiled resource in the System.Resources namespace. Any resources in the App_GlobalResources folder are automatically compiled into this namespace.

The Label2 control is set programmatically using the GetGlobalResourceObject method of the TemplateControl class.

The Label3 control is set declaratively using explicit resource expression syntax.

 

How to: Migrate an ASP.NET 1.x Stand-Alone Class that Accesses another Control’s Code-Behind Class 

This topic demonstrates how to migrate an ASP.NET version 1.x stand-alone class to ASP.NET version 2.0. The stand-alone class example used in the following procedures accesses a static field in another control’s code-behind class.

In previous versions of ASP.NET, all pages in a Web application are compiled into a single assembly so that references from the stand-alone class to the code-behind class can be resolved. In ASP.NET 2.0, the page compilation, by default, results in multiple assemblies; references from the stand-alone class to the code-behind class will result in a compilation error. This topic demonstrates a coding pattern to enable a Web application that contains stand-alone classes that reference code-behind classes to successfully compile and run. The pattern uses an abstract base stub class in the App_Code directory.

The procedures in this topic require the .NET Framework version 2.0.

To access a code-behind class using the ASP.NET 1.x coding pattern

1.           In the root of your Web application, create a folder called Controls. In this folder, create a user control with a code-behind class, as shown in the following example.

Visual Basic

<%@ Control Language=”VB” AutoEventWireup=”false” CodeBehind=”CodeBehindExampleUserControl.ascx.cs” Inherits=”CodeBehindExampleUserControl” %>

C#

<%@ Control Language=”C#” AutoEventWireup=”false” CodeBehind=”CodeBehindExampleUserControl.ascx.cs” Inherits=”CodeBehindExampleUserControl” %>

Visual Basic

Public Class CodeBehindExampleUserControl Inherits System.Web.UI.UserControl Public Shared Name As String = “CodeBehindExampleUserControl” Protected Sub Page_Load(ByVal sender As Object, _ ByVal e As System.EventArgs) Handles Me.Load ‘ User control code. End Sub End Class

C#

public class CodeBehindExampleUserControl : System.Web.UI.UserControl { public static string Name = “CodeBehindExampleUserControl”; protected void Page_Load(object sender, System.EventArgs e) { // User control code. } }

2.           In the root of your Web application, create a stand-alone class as shown in the following example.

Visual Basic

Imports Microsoft.VisualBasic Public Class StandAlone Private _name As String = “StandAlone” Public ReadOnly Property Name() As String Get Return _name & ” + ” & CodeBehindExampleUserControl.Name End Get End Property End Class

C#

public class StandAlone { private string _name; public StandAlone() { // constructor _name = “StandAlone”; } public string Name { get { return _name + ” + ” + CodeBehindExampleUserControl.Name; } } }

3.           In the root of your Web application, create a Web page with a code-behind class as shown in the following example.

Visual Basic

<%@ Page Language=”VB” AutoEventWireup=”false” CodeBehind=”CodeBehindExample.aspx.vb” Inherits=”CodeBehindExample” %> <!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN” “http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd”&gt; <html > <head id=”Head1″ runat=”server”> <title>Code-Behind Example</title> </head> <body> <form id=”form1″ runat=”server”> <div> <asp:Label id=”Label1″ runat=”server”></asp:Label> </div> </form> </body> </html>

C#

<%@ Page Language=”C#” AutoEventWireup=”true” CodeBehind=”CodeBehindExample.aspx.cs” Inherits=”CodeBehindExample” %> <!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN” “http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd”&gt; <html > <head id=”Head1″ runat=”server”> <title>Code-Behind Example</title> </head> <body> <form id=”form1″ runat=”server”> <div> <asp:Label id=”Label1″ runat=”server”></asp:Label> </div> </form> </body> </html>

Visual Basic

Public Class CodeBehindExample Inherits System.Web.UI.Page Protected Label1 As System.Web.UI.WebControls.Label Protected Sub Page_Load(ByVal sender As Object, _ ByVal e As System.EventArgs) Handles Me.Load Dim c As New StandAlone() Label1.Text = c.Name End Sub End Class

C#

public class CodeBehindExample : System.Web.UI.Page { protected System.Web.UI.WebControls.Label Label1; protected void Page_Load(object sender, System.EventArgs e) { StandAlone c = new StandAlone(); Label1.Text = c.Name; } }

4.           Create a Bin folder in your Web application if one does not already exist, and compile the Web page, user control, and stand-alone class into an assembly as shown in the following example.

Visual Basic

vbc /target:library /nologo /out:bin\CodeBehindExampleVB.dll CodeBehindExample.aspx.vb controls\CodeBehindExampleUserControl.ascx.vb StandAlone.vb

C#

csc /target:library /nologo /out:bin\CodeBehindExampleCS.dll CodeBehindExample.aspx.cs controls\CodeBehindExampleUserControl.ascx.cs StandAlone.cs

5.           Request the Web page CodeBehindExample.aspx in a browser to verify that it works in the .NET Framework 2.0.

The page should return a concatenated string, “StandAlone + CodeBehindExampleUserControl”. The CodeBehindExample page creates an instance of a new StandAlone class. The StandAlone class in turn accesses the Name field of the CodeBehindExampleUserControl user control.

To access a code-behind class using the ASP.NET 2.0 coding pattern

1.           Remove the assembly created in the previous procedure.

2.           In the user control created in the previous procedure, make changes so that the user control resembles the following example.

Visual Basic

<%@ Control Language=”VB” AutoEventWireup=”false” CodeFile=”CodeBehindExampleUserControl.ascx.vb” Inherits=”CodeBehindExampleUserControl_Migrated” %>

C#

<%@ Control Language=”C#” AutoEventWireup=”true” CodeFile=”CodeBehindExampleUserControl.ascx.cs” Inherits=”CodeBehindExampleUserControl_Migrated” %>

Visual Basic

Partial Class CodeBehindExampleUserControl_Migrated Inherits CodeBehindExampleUserControl Protected Sub Page_Load(ByVal sender As Object, _ ByVal e As System.EventArgs) Handles Me.Load ‘ User control code. End Sub End Class

C#

public partial class CodeBehindExampleUserControl_Migrated : CodeBehindExampleUserControl { protected void Page_Load(object sender, System.EventArgs e) { // User control code. } }

You are using a partial class in the code-behind file and the CodeFile attribute of the @ Page directive in the .ascx file. Additionally, the code-behind partial class inherits from a class called CodeBehindExampleUserControl and not the UserControl class as in the previous procedure. The definition of the CodeBehindExampleUserControl class is described in a subsequent step of this procedure. Finally, note that there is no definition for the static field Name; this is defined in the CodeBehindExampleUserControl class.

3.           Modify the stand-alone class to resemble the following example.

Visual Basic

Imports Microsoft.VisualBasic Public Class StandAlone Private _name As String = “StandAlone” Public ReadOnly Property Name() As String Get Return _name & ” + ” & CodeBehindExampleUserControl.Name End Get End Property End Class

C#

public class StandAlone { private string _name; public StandAlone() { // constructor _name = “StandAlone”; } public string Name { get { return _name + ” + ” + CodeBehindExampleUserControl.Name; } } }

4.           Create an App_Code folder in your Web application if one does not exist. Move the stand-alone StandAlone class definition file to the App_Code folder.

5.           Inside the App_Code folder, create an abstract stub file for the user control. The stub file will define the CodeBehindExampleUserControl class and should resemble the following example.

Visual Basic

Imports Microsoft.VisualBasic Public MustInherit Class CodeBehindExampleUserControl Inherits System.Web.UI.UserControl Public Shared Name As String = “CodeBehindExampleUserControl” End Class

C#

abstract public class CodeBehindExampleUserControl : System.Web.UI.UserControl { public static string Name = “CodeBehindExampleUserControl”; }

The abstract class definition contains a definition of the static field Name. Because the CodeBehindExampleUserControl class is in the App_Code folder, its members and fields are visible to controls in your Web application.

6.           Request the Web page CodeBehindExample.aspx in a browser to verify that the page maintains the functionality it had when it used the ASP.NET 1.x coding pattern.

In the ASP.NET 2.0 coding pattern, you do not need to explicitly compile the classes into an assembly.

 

How to: Run ASP.NET 1.x Applications in the .NET Framework 2.0 

This topic demonstrates how to run an ASP.NET version 1.x Web application in the .NET Framework version 2.0. As a prerequisite, the ASP.NET 1.x Web application should be able to run in the .NET Framework 1.x version with which it was developed.

The .NET Framework allows you to install multiple versions of the common language runtime on the same computer. Installing the .NET Framework 2.0 will not disrupt any existing applications that are running on previous versions of the .NET Framework. The .NET Framework 2.0 is designed to have a high degree of backward compatibility with applications built with previous versions of the .NET Framework. An application running on a previous version of the .NET Framework can be made to run on the .NET Framework 2.0 by mapping the application to use the ASP.NET 2.0 ISAPI extension (Aspnet_isapi.dll). For more information on running multiple versions of the .NET Framework side by side, see ASP.NET Side-by-Side Overview.

Running an ASP.NET 1.x application in the .NET Framework 2.0 allows you to use ASP.NET 2.0 features in your application’s pages. If you choose to Migrate your application, you should review what’s new in ASP.NET, and in particular review changes to the page code-behind model, Web site folder structure, and dynamic page compilation.

To configure a 1.x application’s script map to use the .NET Framework version 2.0

1.           On the Start menu, click Run.

2.           In the Open box, type inetmgr and click OK.

3.           In Internet Information Services (IIS) Manager, expand the local computer, and then expand Web Sites.

4.           Select the target Web site that is running in the .NET Framework version 1.x.

5.           Right-click the name of the virtual directory for the Web site, and then click Properties.

The <WebSiteName> Properties dialog box appears.

6.           In the ASP.NET version selection list, choose the .NET Framework version 2.0.

7.           Click OK.

8.           Navigate to a page in your application and confirm that your application runs as expected.

 

Advertisements

2 Responses to “ASP.NET2.0 Migration Overview”

  1. Most I can express is, I’m not sure what to say! Except needless to say, for the excellent tips that happen to be shared using this blog. I will think of a thousand fun approaches to read the reports on this site. I believe I will at last make a move using your tips on those issues I could never have been able to deal with alone. You were so thoughtful to let me be one of those to learn from your useful information. Please recognize how great I appreciate it.

  2. Thanks for taking the time to debate this, I really feel strongly about it and love learning extra on this topic. If potential, as you gain expertise, would you mind updating your weblog with further data? It is extremely useful for me.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
%d bloggers like this: