Work With Multiple Serverside Languages

You can implement ASP.NET applications that use multiple server-side languages. The .NET platform supports several languages and is architected to support any mainstream language that can supply a compiler that will generate IL (Intermediate Language) that is compatible with the CLR (Common Language Runtime). Also, if the language is to interoperate with other .NET languages, it must be CLS (Common Language Specification) compliant.

The .NET platform does support multiple languages, and ASP.NET applications can use these CLR complaint languages, but the Web pages only support one server-side language per page. The one language per Web page applies only to code processed on the server. You can still have multiple client-side languages. ASP.NET treats the client-side language just like normal HTML markup and lets the user's browser interpret the script. Client-side language support is handled by the browser, just as with ASP 3.0 applications.

To indicate what language you want to run on a page for server-side code processing, you can set it with the Language attribute on the @Page directive. If the language is not specified, ASP.NET assumes that the language is VB.

WORK WITH MULTIPLE SERVER-SIDE LANGUAGES

WORK WITH MULTIPLE SERVER-SIDE LANGUAGES

D Open

MultipleLanguages.aspx from the Code Templates directory.

■ The page displays two server-side script blocks using two different languages.

0 Save the file and request it from the Web server.

■ The JScript code and the VBScript code execute.

D Open

MultipleLanguages.aspx from the Code Templates directory.

■ The page displays two server-side script blocks using two different languages.

0 Save the file and request it from the Web server.

■ The JScript code and the VBScript code execute.

MIGRATE FROM ASP TO ASP.NET

ASP.NET Web pages are compiled on the Web server. This is why there is support for only one language per page. This is different than traditional ASP pages, which are normally interpreted each time they are requested.

If the language is not specified in the @Page directive and you only have server-side code in a <script> tag, then you could set the Language attribute for the script element.

The .NET Framework provides the Common Language Specification (CLS) to ensure that .NET compliant language can interoperate. CLS describes a fundamental set of language features and defines rules for how those features are used. CLS-compliant languages enable you to do such things as inherit classes from other CLS-compliant classes and pass data types without having to do any special preparation (like buffering a string).

When you know how to program in a language that is CLS compliant, you can leverage this knowledge by creating other applications that run on the CLR (like a Windows Forms application, a Web Service, a Mobile application, or a distributed application).

S Open

MultipleLanguages.aspx from the Code Templates directory.

■ The page displays two server-side script blocks using two different languages.

File Edit View Favorites Tools Help

Address http: //localhost/M ultipleLanguages, aspx

Line 15: <SCRIPT LANGUAGE="UB" RHNAT="Server">

Line 16: Response.Write "VB code is being executed."

Source File: c:\inetpub\wwwroot\Multiplel_anguages.aspx Line: 15

S Open

MultipleLanguages.aspx from the Code Templates directory.

■ The page displays two server-side script blocks using two different languages.

- Microsoft Internet Explorer

File Edit View Favorites Tools Help ira vi-. Back ^ -» - [p ffi [EjPersonal Bar Search Favorites 0 | %■* # E3 T M %

Address http: //localhost/M ultipleLanguages, aspx

Server Error in T Application

Parser Error Message: Cannot use language 'VB' because another language has been specified earlier in this page

Source Error:

Line 15: <SCRIPT LANGUAGE="UB" RHNAT="Server">

Line 16: Response.Write "VB code is being executed."

Source File: c:\inetpub\wwwroot\Multiplel_anguages.aspx Line: 15

□ Save the file and request it from the Web server.

|jä? Local intranet

■ An error message appears because more than two languages cannot be used on the same page.

You can embed server-side code in ASP.NET and ASP applications with <script> blocks. This feature has not changed from ASP applications to ASP.NET applications. Server-side <script> blocks were available and configured the same way. What is new is the concept of using code-behind pages in ASP.NET pages. See page 192 for a cleaner way to implement server-side code.

What you need to be aware of with <script> blocks is that they are necessary for procedures that are placed inside of the ASP.NET page. Before, with ASP

To include a server-side <script> block, you need to (at a minimum) put in the runat="Server" attribute. You also should be explicit with the language you are implementing inside of the <script> block. The default is VB. So, if you desire to use C#, you will need to put the Language="C#" attribute inside the script element.

WORK WITH SCRIPT BLOCKS

WORK WITH SCRIPT BLOCKS

D Open

ScriptBlocks.aspx from the Code Templates directory.

■ The page displays the SayWelcomeStatement that outputs the header for the page.

0 Save the file and request ScriptBlocks.asp from the Web server.

■ The ASP code executes without issues.

D Open

ScriptBlocks.aspx from the Code Templates directory.

■ The page displays the SayWelcomeStatement that outputs the header for the page.

0 Save the file and request ScriptBlocks.asp from the Web server.

■ The ASP code executes without issues.

MIGRATE FROM ASP TO ASP.NET

You can put scripts into files that are external to your ASP.NET page. You can do this with the <script> element by specifying an external script file using the src attribute. When you define the src attribute, all content between the opening and closing tags of the <script> element is ignored. Because this is the case, it is best to define the <script> element as an empty tag. For example, <script runat="server" src="scrStandard.cs" />.

With ASP.NET, you must declare global variables within <script runat=server> blocks and not between code delimiters

0 Open

ScriptBlocks.aspx from the Code Templates directory.

■ The page displays the SayWelcomeStatement that outputs the header for the page.

□ Save the file and request it from the Web server.

■ An error message appears because parentheses were not used when calling the subprocedure.

0 Open

ScriptBlocks.aspx from the Code Templates directory.

■ The page displays the SayWelcomeStatement that outputs the header for the page.

□ Save the file and request it from the Web server.

■ An error message appears because parentheses were not used when calling the subprocedure.

USING RENDER FUNCTIONS

Render functions that were in your ASP applications can be replaced with global functions in ASP.NET. Traditional render functions that were available in ASP applications are no longer available in ASP.NET applications. Render functions were functions that embedded HTML inside of the function. This HTML was not written with the Response.Write; it was embedded inside of the function. This was done by starting the subroutine (<% Sub RenderHeader() %>) with an open code deliminator and ending with a close code deliminator. Then the HTML would be written as though it were outside of the function (<H1>Welcome to www.mylifetimegoals.com</H1>). Finally, the subroutine would be closed with code deliminators (<% End Sub %>). This render function could be called conditionally after the subroutine or function definition (<% Call RenderHeader %>).

This ASP trick is no longer available in ASP.NET. There are two issues with render functions that violate ASP.NET syntax rules. First, HTML can not be embedded in a function. To resolve this issue, use Response.Write. Second, procedures cannot reside inside of code delimiters (<% and %>). This is resolved by putting the function or subroutine in the serverside <script> blocks.

USING RENDER FUNCTIONS

USING RENDER FUNCTIONS

D Open

RenderFunctions.aspx from the Code Templates directory.

■ The page displays the SayWelcomeStatement that outputs the header for the page.

0 Save the file and request it ■ The ASP code is able to from the Web server. use the render function.

D Open

RenderFunctions.aspx from the Code Templates directory.

■ The page displays the SayWelcomeStatement that outputs the header for the page.

0 Save the file and request it ■ The ASP code is able to from the Web server. use the render function.

MIGRATE FROM ASP TO ASP.NET

Extra

Rendering functions were commonplace in ASP applications. Using rendering functions to build tables from recordsets was very convenient, but it created very messy code that was hard to troubleshoot. Because rendering functions are not allowed in ASP.NET, you can either put the functions in a script block using the Response.Write ("html goes in here") method, or you can use the data binding capabilities that come with the ASP.NET framework. Refer to Chapter 6 for data binding to server-side controls. This gives you the ability to populate a table with much less code that is easier to follow.

S Open

RenderFunctionsl.aspx from the Code Templates directory.

■ The page displays the SayWelcomeStatement that outputs the header for the page.

□ Save the file and request it from the Web server.

■ An error message appears because a render function was used.

S Open

RenderFunctionsl.aspx from the Code Templates directory.

■ The page displays the SayWelcomeStatement that outputs the header for the page.

□ Save the file and request it from the Web server.

■ An error message appears because a render function was used.

USING PAGE DIRECTIVES

The standard set of processing directives for an ASP.NET page has changed dramatically from what was available with ASP. When migrating from ASP to ASP.NET, you need to update your pages to use the @Page directive to let ASP.NET know certain processing requirements for an ASP.NET Web page. For example, in ASP pages, if you wanted to specify the server-side language, you used the @Language directive. Now, with ASP.NET Web pages, you must use the Language attribute that is available via the @Page directive.

The @Page directive contains many of the directives that were available for ASP Applications. lcid and

CodePage attributes on the @Page directive for ASP.NET Web pages are all directives that must be placed on the first line of a page within the same delimiting block, for example, ASP.NET <%@Page Language="VB" CodePage="932"%> versus ASP <%@ language="vbscript" CODEPAGE="932"%>.

With ASP.NET Web pages, you can have as many lines of directives as you need. Standard practice is to put directives at the top of the page, but you are able to put them anywhere in your ASP.NET Web page.

USING PAGE DIRECTIVES

USING PAGE DIRECTIVES

D Open

PageDirective.aspx from the Code Templates directory.

■ The page does not use the @Page directive. It uses the @Language directive without the @Page.

H Save the file and request it from the Web server.

■ The ASP code executes displaying the header that is contained in a subroutine.

D Open

PageDirective.aspx from the Code Templates directory.

■ The page does not use the @Page directive. It uses the @Language directive without the @Page.

H Save the file and request it from the Web server.

■ The ASP code executes displaying the header that is contained in a subroutine.

MIGRATE FROM ASP TO ASP.NET

When migrating ASP applications to ASP.NET applications, you have other directives besides the @Page directive. For example, you can use the @OutputCache directive to control how a page is cached on the server. With page caching on the server, results of a processed .aspx page are held in memory at the server. The next time the page is requested, the cached page can be sent as the response instead of regenerating the page. This can give you excellent performance gains, especially when the page generation involves calls to other machines (like database servers). You can control the location of where the page is cached via the Location attribute. The Location attribute has the following options:

OPTION

LOCATION

Any

Client, Downstream, or Server

Client

Browser client where the request originated

Downstream

A server downstream from the Web server that processed the request

None

N/A

Server

Web server where the request was processed.

S Open

PageDirective.aspx from the Code Templates directory.

■ The page uses the @Page directive to set the server-side language.

□ Save the file and request it from the Web server.

■ The ASP code executes using the @Page directive to determine the server-side language.

S Open

PageDirective.aspx from the Code Templates directory.

■ The page uses the @Page directive to set the server-side language.

□ Save the file and request it from the Web server.

■ The ASP code executes using the @Page directive to determine the server-side language.

For your server-side code, you can migrate VBScript code to VB.NET in your ASP.NET applications. VBScript is the most common language used for ASP Applications. If you decide to migrate an ASP application that used VBScript for server-side code, you can make some minor modifications to VBScript to turn it into VB code that will run in the Common Language Runtime (CLR).

If you migrate an existing ASP application to an ASP.NET application, you will have to make some decisions on how far you want to take your conversion process. At a high level, you can take one of three paths depending on the number of changes you want to make. The first path is to rewrite your entire application, treating you ASP application as a prototype. This has the most cost initially, but it may be the cheapest option in the long run. The second option is to just convert the ASP pages (*.asp) to ASP.NET pages (*.aspx) — making the minimal amount of changes required to give the file an aspx extension. The third option, initially the cheapest cost of migration, is to leave the file as an ASP page, keeping the extension as asp. A migration project can involve one or more of these paths. Most likely, you will treat this on a page-by-page basis.

MIGRATE VBSCRIPT TO VB.NET SYNTAX

MIGRATE VBSCRIPT TO VB.NET SYNTAX

D Open

SimpleQuery_VBScript.aspx from the Code Templates directory.

0 Scroll down the file to view the code.

S Save the file and request SimpleQuery_VBScript.aspx from the Web server.

■ The ASP code executes without issues and displays data from the database of the author.

D Open

SimpleQuery_VBScript.aspx from the Code Templates directory.

0 Scroll down the file to view the code.

S Save the file and request SimpleQuery_VBScript.aspx from the Web server.

□ Copy the file from the CD-ROM to the working directory.

■ The ASP code executes without issues and displays data from the database of the author.

MIGRATE FROM ASP TO ASP.NET

Exlr Microsoft has made VB a first class language, keeping the following in mind:

• Making the language more consistent: bringing together features of the language with similar purpose.

• Simplifying the language: redesigning those features which made Visual Basic anything less than "basic."

• Improving readability and maintainability: redesigning features that hide too many important details from the programmer.

• Improving robustness: enforcing better practices, such as type-safe programming.

In VB.NET, the On Error Resume Next and On Error-Goto error handling is still available. Even though this is still available, you should take advantage of structured error handling that is available for all CLS (Common Language Specification) compatible languages. This uses the Try, Catch, and Finally keywords.

^Compilation Erior - Microsoft Internet Explorer

I File Edit View Favorites Tools Help f-ll

vr3 Back - tâ g] $ I Personal Bar ^Search

11 Favorites 0 | # 13 - M %

Address http://localhost/SimpleQuerii_VBScript.aspx

Links »

Compilation Error

Description: An error occurred during the compilation of a resource required to service this request. Please review the specific error details below and modify your source code appropriately.

Compiler Error Message: BC30024: Invalid inside procedure

Source Error:

Line 1: <%0 LANGUAGE = VBScript

Line 4: <HTML>

-

Source File: c:\inetpub\wwwroot\SimpleQueryJ

/BScript.aspx Line: 2

CDone

Local intranet |

B Save the file with an .aspx extension and then request it from the Web server.

■ An error message appears because the Option Explicit is not allowed.

B Change the language in the source file from VBScript to VB, add the keyword Page to the directive at the top of the file, and remove the Option Explicit code.

Q Request

SimpleQuery_VBScript.aspx from the Web server.

■ An error message appears because the Let and Set statements are no longer supported.

CONTINUED

Not all ASP pages need to be changed when converting to an ASP.NET application. However, for the ASP pages that you will convert to ASP.NET, you have to first change the extension from asp to aspx. After the extension is changed, you must change the processing directives on the ASP page. Most of the directives on an ASP page will migrate to the @Page directive, for example, Language=VBScript will change to @Page Language=VB. See page 280 for further details on the @Page directive).

A big change from VBScript to VB.NET is the removal of the variant type. In VBScript, you did not have the ability to strongly type variables, but you would use Option Explicit to avoid using variables that were not declared. If you mistyped a variable name and did not use Option Explicit, you could generate some interesting bugs. Option Explicit is no longer needed because it is the default for ASP.NET pages.

If you decide to interoperate with COM objects, you need to be aware that ASP.NET's threading model is the Multiple Threaded Apartment (MTA). This means that a standard ASP.NET page will not be able to use a normal COM object created by VB6, which is Single Threaded Apartment (STA). To correct this you can set AspCompat="true" for the @Page directive.

MIGRATE VBSCRIPT TO VB.NET SYNTAX (CONTINUED)

MIGRATE VBSCRIPT TO VB.NET SYNTAX (CONTINUED)

'-Q Remove the two Set statements ■ An error message from the source file.

• Save the file and request SimpleQuery_VBScript.aspx from the Web server.

appears because VB requires the argument list to be enclosed in parentheses.

Place parentheses around the statement

"Provider=Microsoft.Jet. OLEDB.4.0;Data Source=" & filePath.

■ An error message appears because you cannot create apartment threaded COM components without the aspcompat=true statement in the @Page directive.

'-Q Remove the two Set statements ■ An error message from the source file.

• Save the file and request SimpleQuery_VBScript.aspx from the Web server.

appears because VB requires the argument list to be enclosed in parentheses.

Place parentheses around the statement

"Provider=Microsoft.Jet. OLEDB.4.0;Data Source=" & filePath.

— Save the file and request SimpleQuery_VBScript.aspx from the Web server.

■ An error message appears because you cannot create apartment threaded COM components without the aspcompat=true statement in the @Page directive.

MIGRATE FROM ASP TO ASP.NET

If you are not explicit in how parameters are passed, you could have issues with your code. In VB6, parameters were, by default, passed by reference (ByRef). This has changed to by value (ByVal) in VB.NET. If your code relied on ByRef parameters, then you could introduced bugs with this change.

VB.NET no longer provides support for default properties. To account for this, you need to explicitly call the property from the object.

Example:

Response.

Write

oRS("FirstName")

'Changes

to:

Response.

Write

(oRS("FirstName").Value)

All of your server-side code method calls will require parentheses.

Example:

Response.Write

'An error has occurred."

'Changes to:

Response.Write("An error has occurred.")

™ Add .Value to the end of the oRs(Index) statement.

■ The page displays properly as an ASP.NET page.

L-E Add aspcompat=true to the

@Page directive at the top of the page.

E Save the file and request SimpleQuery_VBScript.aspx from the Web server.

■ The page renders without issues but does not display the data because the default properties are not supported in VB.

™ Add .Value to the end of the oRs(Index) statement.

£ Save the file and request SimpleQuery_VBScript.aspx from the Web server.

■ The page displays properly as an ASP.NET page.

Was this article helpful?

0 0

Post a comment