Mixing Languages in ASP.NET Application

ASP.net 2.0 apps can host more than one language, but you have to set up your project correctly. Dino shows you how


May 22, 2007
URL:http://www.drdobbs.com/windows/mixing-languages-in-aspnet-application/199701178

Each ASP.NET 2.0 page is associated with a single programming language—be it Visual Basic .NET, C#, or any other language for which you hold a .NET compiler. But what about ASP.NET applications? Does the restriction on the single programming language remain? Fortunately not. ASP.NET 2.0 you can mix together, say, C# and Visual Basic .NET in the same application, although not in the same page. Let's drill down.

An ASP.NET page—an ASPX source file—is parsed to a class file and then compiled to an assembly. For this to happen, a programming language is required. The markup in the ASPX source file must be parsed to a language class—whatever language, but just one. This dynamically created page class is then compiled to an assembly and used to serve a response to a client browser.

You can designate the page language in a few ways. For example, you just set the Language attribute on the @Page directive at the very top of an ASP.NET page. By the way, this is the most common strategy and the one used by Visual Studio 2005. When you add a new ASP.NET Web form to a project, here's what the page contains:


<%@ Page Language="C#" ... %>

The page is bound to the C# language, meaning that C# has to be the language of the code-behind class and C# is the only supported language for any server script you insert inline in the markup.


<script runat="server" language="C#">
   .
   .
</script>

Server-side code snippets are merged as methods in the resulting page class file; as such they can't be written in a different programming language than the designated page language. In other words, server code snippets are not compiled separately, but are fused to the same source, then compiled in a single step. Clearly, the programming language are to be the same. If you omit to specify the language attribute in the @Page directive, then the language is determined by looking at the language attribute of the first server-side script tag. If no server script tag exists, or if no language is explicitly set in any of them, then the language for the page defaults to Visual Basic .NET.

This behavior is the same for any version of ASP.NET. This means that two languages can't be mixed up in the same page; but distinct pages in the same application can use different languages.

In ASP.NET 2.0, you can define an App_Code folder below the root of your site. This folder is designed to contain helper class files to be compiled together in a bunch of assemblies. What about languages? Ideally, all files in App_Code and all of its subfolders are to be written in the same language. This is not a strict rule, however.

If you have class files written in two or more languages, you must create language-specific App_Code subdirectories to contain classes for each language you support. Once files have been grouped by language, you add an entry to the web.config file for each subdirectory:


<compilation>
    <codeSubDirectories>
        <add directoryName="VB" />
    </codeSubDirectories>
</compilation>

It is important that the language-specific subdirectory is registered in the web.config file; otherwise, all files underneath App_Code will be compiled to a single assembly regardless of the folder they belong.

The preceding configuration script delineates a situation in which all, say, VB files are located in the /VB directory and everything else under the App_Code tree is C#. If a directory mentioned in the section doesn't exist, you'll receive a compile error.

Using multiple languages in the same ASP.NET project is helpful in at least a couple of scenarios—when you're integrating existing code and when you have developers on the project with different skills. Using different languages is definitely possible; use it at your best convenience.

Terms of Service | Privacy Statement | Copyright © 2024 UBM Tech, All rights reserved.