Multilanguage Projects with Visual Studio .NET

Understand the role of the undocumented CodeBehind attribute in ASP.NET's @Page directive


December 09, 2002
URL:http://www.drdobbs.com/multilanguage-projects-with-visual-studi/184416792

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].

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