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

Multilanguage Projects with Visual Studio .NET


MultiLanguage Projects with Visual Studio .NET

ASP.NET projects created with Visual Studio .NET don’t allow you to use multiple languages. In other words, you can’t have .aspx pages that use Visual Basic .NET and .aspx pages that use C# in the same application. Note, though, that this is a Visual Studio .NET limitation, not an ASP.NET limitation. Visual Studio also forces you to develop web pages using the code-behind approach, meaning that all the page information is split into two distinct files—one with layout data and controls declaration and one (the code-behind file) with code and event handlers. All codebehind files, and subsequently all pages, must be written in the language of the project.

To work around the issue, you can write your pages (in any language) without the code-behind feature and at a later time import them in the project using the Add Existing Item menu. In this case, though, you have all the page-specific code concentrated in the <script> section. In addition, no IntelliSense and syntax-coloring facilities will be provided to edit the <script> section.

When you add an existing page to a project, Visual Studio looks for a code-behind class and offers to create a new one. The code-behind class cannot be any source or compiled class that inherits from Page. To meet the requirements of Visual Studio, a code-behind class must fulfill a special naming convention and be written in the same language used throughout the project. For example, a page called MyPage.aspx can only have a code-behind class called MyPage.aspx.cs, if C# is the project’s language. Again, this is a Visual Studio limitation but not an ASP.NET shortcoming

Visual Studio .NET tracks the relationship between the page and the code-behind class through an undocumented CodeBehind attribute in the @Page directive. The content of the CodeBehind attribute cannot be edited from within Visual Studio .NET and, if you edit it with another text editor, you get an error as soon as you reload the page within Visual Studio .NET.

Visual Studio utilizes the CodeBehind attribute only for internal editing purposes. This probably explains why the attribute is not even mentioned in the MSDN documentation. CodeBehind is not intended to be used at the application level and, in fact, the ASP.NET runtime just ignores it. Let's consider the following header, typical of an ASP.NET page developed with Visual Studio .NET.

<%@ Page Language="c#" Inherits="WDM.MyBasePage" 
     CodeBehind="MyBasePage.cs" %>

The ASP.NET runtime expects to find the code for the page in a class called WDM.MyBasePage, located in one of the reachable assemblies. There’s no need to have the C# file that is referenced in the CodeBehind attribute in the page’s folder—that piece of information exclusively serves the purposes of the Visual Studio .NET project.

If you want the .aspx page to inherit from a local .cs or .vb file, use the @Page’s Src attribute instead. The Src attribute lets you specify the code-behind through a path. The code of the class will be compiled the first time the page is accessed.

You should note, however, that Visual Studio .NET ignores the Src attribute. Using both Src and CodeBehind in a page does not result in any error, but is a programming practice that I strongly discourage because of the side effects and the confusion it could generate.

What strategy can you use to develop ASP.NET applications using different languages? Group the various pages in multiple language-homogeneous projects. Next, make all projects share the same virtual folder. To keep all projects within reach all the time, group them in the same Visual Studio .NET solution.


Dino Esposito is Wintellect's ADO.NET and XML expert, and a trainer and consultant based in Rome, Italy. Dino is a contributing editor to Windows Developer Magazine and MSDN Magazine, and the author of several books for Microsoft Press including Building Web Solutions with ASP.NET and ADO.NET and Applied XML Programming for .NET. Contact Dino at [email protected].


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.