ASP.NET 2.0 introduces the App_Code directory, which is added by default to every new Web application created using Visual Studio.NET 2005.
This folder can be used to place standalone code files that are to be shared among all the pages inside a single Web application. At run-time, this folder will be compiled and a single assembly is created and made available to all the pages in the application.
A nice feature about this folder is that we can place code from several .NET languages. In other words, we can have objects written in C# and objects written in VB.NET and even other languages that can be used with ASP.NET development.
In this code snippet, we will demonstrate how we can have an ASP.NET 2.0/VB.NET Web application with its own C# and VB.NET objects placed in the App_Code directory.
In this step-by-step demonstration, we will create a new Web application and show you how to set up your application to handle objects from both C# and VB.NET languages.
The first thing to do is to create a new ASP.NET website with Visual Basic as the main language in that application.
Open the Web application configuration file and add the configuration lines shown in listing 1 below to the system.web section.
Listing 1: Configuring Web.config
In this step, we add a new object written in Visual Basic .NET to the App_Code directory. The object will contain only one single function, which is shown in listing 2 below.
Listing 2: VB.NET Object Placed in App_Code
Public Class VBObject
Public Function FormatString(ByVal inputStr As String) As String
Return "Accessed from Vb.NET object " & inputStr
Listing 2 presents a function that takes as input a parameter of type string and returns a string stating the object language from which the function is being called. Notice when you try to create a new object for the first time, Visual Studio.Net will ask you if you want to create an App_Code directory and place the object inside it.
In this step, we create a new directory, called CS, inside the App_Code directory. If you look at the Web.config configuration section where we add a sub-directory, you will see that the folder to be added should have the name of CS, though in fact, it could be named something else.
Its now time to add a new object written in C#. The new object will be added to the CS sub- directory inside the App_Code directory. The object is shown below in listing 3:
Listing 3: C# Oject Placed in CS Directory Inside App_Code Directory
public class CSharpObject
public String FormatString(String inputStr)
return "Accessed from C# object " + inputStr;
In this final step, we will add a new ASP.NET page. This page will be relatively simple; we will add the code inside the Page_Load method of this page. The code instantiates the two objects we created before, and then calls each function in those objects to show you how we can use two objects written in two different languages in the same Web application. The code-behind is shown in listing 4 below:
Listing 4: ASP.NET Code-Behind
Partial Class _Default
Protected Sub Page_Load(ByVal sender As Object, _
ByVal e As System.EventArgs) Handles Me.Load
Dim VBObject1 As New VBObject()
Dim CSharpObject1 As New CSharpObject()
As you can clearly see, using objects from different languages is no longer a problem in ASP.NET 2.0. I hope that you find this code snippet useful and start using several .NET languages inside your Web applications when you find it useful.