C# and VB code mixed together in ASP.NET 2.0

Introduction

In ASP.NET 2.0 you can use C# and VB.NET web forms together in one web site. But if you want to use classes coded in different languages, Such mixing is not allowed by default settings. However you can configure web site to get this done. This article drill down to achieve this.

Create App_Code and Sub Directories with classes

Create a new web site in VS.NET 2005. Add App_Code folder to it.

img1.GIF

Add two class files in App_Code- TestClass1.cs(C# code) and TestClass2.vb(VB Code). Build the application. It will show the following error

ImgUpdate.jpg

To overcome this error add two sub directories(CS,VB) one for C# class and one for VB class

img3.GIF

Now when you try to build the application, it shows the following error

img2.GIF

Here is the trick - If you have class files written in different languages, create language-specific App_Code subdirectories to contain classes for each language. Add entry to the web.config file for each subdirectory. It is important that the language-specific subdirectory is registered in the web.config otherwise, all files under App_Code will be compiled to a single assembly regardless of the folder they belong.

<system.web>
<compilation>
<codeSubDirectories>
<
add directoryName="CS"/>
<add directoryName="VB"/>
</
codeSubDirectories>
</
compilation>

Using multiple languages in the same ASP.NET project is helpful when you're integrating existing code or when you have developers on the project with different skills.


Similar Articles