Dr. Dobb's is part of the Informa Tech Division of Informa PLC

This site is operated by a business or businesses owned by Informa PLC and all copyright resides with them. Informa PLC's registered office is 5 Howick Place, London SW1P 1WG. Registered in England and Wales. Number 8860726.


Channels ▼
RSS

.NET

Mixing Languages in ASP.NET Application


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.


Related Reading


More Insights






Currently we allow the following HTML tags in comments:

Single tags

These tags can be used alone and don't need an ending tag.

<br> Defines a single line break

<hr> Defines a horizontal line

Matching tags

These require an ending tag - e.g. <i>italic text</i>

<a> Defines an anchor

<b> Defines bold text

<big> Defines big text

<blockquote> Defines a long quotation

<caption> Defines a table caption

<cite> Defines a citation

<code> Defines computer code text

<em> Defines emphasized text

<fieldset> Defines a border around elements in a form

<h1> This is heading 1

<h2> This is heading 2

<h3> This is heading 3

<h4> This is heading 4

<h5> This is heading 5

<h6> This is heading 6

<i> Defines italic text

<p> Defines a paragraph

<pre> Defines preformatted text

<q> Defines a short quotation

<samp> Defines sample computer code text

<small> Defines small text

<span> Defines a section in a document

<s> Defines strikethrough text

<strike> Defines strikethrough text

<strong> Defines strong text

<sub> Defines subscripted text

<sup> Defines superscripted text

<u> Defines underlined text

Dr. Dobb's encourages readers to engage in spirited, healthy debate, including taking us to task. However, Dr. Dobb's moderates all comments posted to our site, and reserves the right to modify or remove any content that it determines to be derogatory, offensive, inflammatory, vulgar, irrelevant/off-topic, racist or obvious marketing or spam. Dr. Dobb's further reserves the right to disable the profile of any commenter participating in said activities.

 
Disqus Tips To upload an avatar photo, first complete your Disqus profile. | View the list of supported HTML tags you can use to style comments. | Please read our commenting policy.