Using Multiple Programming Languages to Create an ASP.NET Website

Did you know it is possible to use more than one programming language in a .NET website project? However, you cannot directly put source code files of different programming languages in the App_Code folder of your ASP.NET website project. by doing so, you would not be able to compile the source code files written in the different languages. In this .NET programming tutorial, we will show you how to create a .NET web app that can make use of more than one programming language.

Read: C# Tools for Code Quality

As an example, let’s say you have created a .NET website project in C# and also want to include another source code file that is written in Visual Basic (VB) into the same project. In this scenario, adding the two distinct source code files into the App_Code folder would not work. In order to make your ASP.Net website work based off more than one language, one solution would be, if the source-code file is small, a developer could convert the VB code into C# code on their own, or use any automated code converter to perform the same action.

However, if that source-code file is larger, or you want to use multiple programming languages for some reason (for example, if one language had features another did not natively have), then converting code from one programming language to another would be very time-consuming and an ineffective task.

Fortunately, there are other methods programmers can use to achieve this goal. We will start by creating a website from scratch, then move on to creating language-specific classes, make some changes in the website configuration, and, finally, test the functionality of our website.

How to Create an ASP.NET Website

To begin, let’s create the framework for our website using the Visual Studio integrated development environment (IDE). Follow the steps below:

  • Launch the Visual Studio IDE and create a new project by clicking on Create a new project.
  • Select ASP.NET Web Application (.NET Framework). Choose the programming language C# from the language dropdown option and click Next.

ASP.NET Web Development

  • On the next screen, configure your website project name and directory and then click Next.
  • Now you should see your website project template has been created by Visual Studio.

Since you are working out of Visual Studio, you may want to check out the features of the new .NET Coding Pack, to add even more features to your code editor.

Creating language-specific Classes in .Net

In this part of the process, we will create two class files in two different languages: C# and Visual Basic. Following are the steps required to create two distinct classes in the App_Code folder:

  • In the Solution Explorer, create two separate folders for each of the programming languages.
  • In this tutorial, we are going to create two subfolders called CS and VB, and add two source code files named ClassCS.cs and ClassVB.vb into them.
  • Add the following source code into the ClassCS.cs file by overwriting the existing source code:
public class ClassCS 
{ 
   public string Message; 
   public string GetMessage() 
   { 
      return this.Message; 
   } 
}

Next, add the following source-code into the ClassVB.vb file by overwriting the existing source-code:

Public Class ClassVB
 
    Public Message As String
 
    Public Function GetMessage() As String
        Return Me.Message
    End Function
 
End Class

Now you are ready to move onto the next step, which is modifying the configuration of our newly created ASP.Net website.

Modifying Website Configuration in ASP.Net

After creating the two separate files for two different programming languages in steps above, the next step is to change the website configuration so that ASP.NET can compile the two distinct folders separately. To change the configuration, follow the below steps:

  • Open the web.config file and locate the section in the file.
  • Add the section, as written below, inside the section.
   
      
      
   

Testing the Functionality of .NET Classes

After the website is configured successfully, let us now test if .NET can compile our two distinct source code files. To check if everything is working well, take the following steps:

  • Create a Default.aspx page in your project folder.
  • Create a label and two button controls on the Default.aspx page.
  • On the click-event of the first button, we want to call the function written in ClassCS. Similarly, on the click-event of the second button, we want to call the function written in ClassVB. The code should look as follows:
protected void Button1_Click(object sender, EventArgs event)
{
   ClassCS obj = new ClassCS();
   obj.Message = "C# Function is called!";
   Label2.Text = obj.GetMessage();
}
 
protected void Button2_Click(object sender, EventArgs event)
{
   ClassVB obj = new ClassVB();
   obj.Message = "VB Function is called!";
   Label2.Text = obj.GetMessage();
}

  • Next, in the Solution Explorer, right-click the Default.aspx page and select Set as Start Page and run the website project.
  • In the last step, we want to test our classes. Call the function of a specific class on the click of the corresponding button. For instance, if you click on the second button, you should see the message: ‘VB Function is called!’

ASP.NET coding examples

Final Thoughts on ASP.Net Muti-langage Web Pages

Using multiple programming languages in ASP.NET is useful in situations where developers and programmers are working independently and they may be using different languages, according to their preferences to support multiple development teams. We hope you have now learned how to support multiple programming languages while building ASP.NET web projects.

Read more ASP.NET software development and programming tutorials.

Tariq Siddiqui
Tariq Siddiqui
A graduate in MS Computer Applications and a Web Developer from India with diverse skills across multiple Web development technologies. Enjoys writing about any tech topic, including programming, algorithms and cloud computing. Traveling and playing video games are the hobbies that interest me most.

More by Author

Must Read