Walkthrough: Using Multiple Programming Languages in a Web Site Project
By default, the App_Code folder does not allow multiple programming languages. However, in a Web site project you can modify your folder structure and configuration settings to support multiple programming languages such as Visual Basic and C#. This allows ASP.NET to create multiple assemblies, one for each language. For more information, see Shared Code Folders in ASP.NET Web Site Projects. Developers commonly include multiple programming languages in Web applications to support multiple development teams that operate independently and prefer different programming languages.
This walkthrough explains how to add multiple programming languages to an ASP.NET application.
Creating the Web Site Project
If you have already created a Web site project in Visual Studio (for example, by completing Walkthrough: Creating a Basic Web Forms Page in Visual Studio), you can use that Web site project and go to the next section. Otherwise, create a new Web site project and page.
To create a file system Web site project
Open Visual Studio.
On the File menu, click New, and then click Web Site. If you are using Visual Studio Express, on the File menu, click NewWeb Site.
The New Web Site dialog box appears.
Under Installed Templates, click the language that you prefer to work in, and then click ASP.NET Web Site.
In the first Location box, select File System. In the second, enter the name of the folder where you want to keep the pages of your Web site project.
For example, enter the folder name C:\WebSites\BulkUpdate.
Click OK.
Visual Studio creates the default folders and pages, including a new page named Default.aspx.
Creating Language-Specific Classes
In this part of the walkthrough, you will create simple class files in two languages, Visual Basic and C#.
To add language specific class files to the App_Code folder
If your Web site project does not already have an App_Code folder, do the following:
In Solution Explorer, select the name of the Web site project.
In the Website menu, click Add ASP.NET Folder, and then click App_Code.
In Solution Explorer, right-click the App_Code folder and then click New Folder.
Name the new folder "CSCode".
Select the CSCode folder.
In the Website menu, click Add New Item.
The Add New Item dialog box is displayed.
In the Add New Item dialog box, choose the Class template, name the class "CSExample", select C# as the language, and click Add.
In Solution Explorer double-click the CSExample.cs file to open it.
Add the following code to the CSExample.cs file, overwriting the existing CSExample code that is already in the file.
public class CSExample { private string teamString; public CSExample() { TeamString = "C# Code"; } public string TeamString { get { return teamString; } set { teamString = value; } } }
Create a folder and class for Visual Basic code by repeating steps 2-7 using the following values:
New folder: VBCode
New class file: VBExample
Note
Be sure to set the language to Visual Basic when creating the new Visual Basic class file.
Add the following code to the VBExample.vb file, overwriting the existing VBExample code that is already in the file.
Public Class VBExample Private teamStr As String Public Sub New() TeamString = "Visual Basic Code" End Sub Public Property TeamString() As String Get Return teamStr End Get Set(ByVal Value As String) teamStr = Value End Set End Property End Class
Modifying the Web.config File
After creating separate subfolders for each programming language, you must change the Web site project configuration so that ASP.NET will compile the subfolders separately.
To modify the Web.config file to support multiple programming languages
In Solution Explorer, select the name of the Web site project.
If your project does not already have a Web.config file, do the following:
In the Website menu, click Add New Item.
Choose Web Configuration File and then click Add.
Double-click the Web.config file to open it.
Modify the <compilation> section to include a <codeSubDirectories> node by copying the following section and pasting it as a child node of the <compilation> section:
<codeSubDirectories> <add directoryName="CSCode"/> <add directoryName="VBCode"/> </codeSubDirectories>
Note
Any definition of this section in Machine.config is overridden by the settings in the Web.config file. Also, the order of the configuration entries is the order that these entries will be created and linked.
Testing the Classes
You can now test that your Web site project can use classes in both programming languages.
To see the results of using multiple programming languages
If your Web site project does not already have a Default.aspx page, do the following:
In Solution Explorer, right-click the Web site project name and then click Add New Item.
Select Web Form, name the page "Default.aspx" and then click Add.
In Solution Explorer double-click the Default.aspx page.
Add a Button control to the Default.aspx page.
Set the Button control's text to "Class Language" and the ID of the button to "classLanguage".
Add a Label control to the Default.aspx page, set its ID property to "classLabel", and clear its Text property.
In Design view, double-click the Button control to create an event handler for its Click event.
Add the following code to the classLanguage_Click handler:
CSExample CSCode = new CSExample(); VBExample VBCode = new VBExample(); if (classLabel.Text == CSCode.TeamString.ToString()) { classLabel.Text = VBCode.TeamString.ToString(); } else { classLabel.Text = CSCode.TeamString.ToString(); }
Dim CSCode As CSExample = New CSExample() Dim VBCode As VBExample = New VBExample() If classLabel.Text = CSCode.TeamString.ToString() Then classLabel.Text = VBCode.TeamString.ToString() Else classLabel.Text = CSCode.TeamString.ToString() End If
In Solution Explorer, right-click Default.aspx and select Set As Start Page.
Run the Web site project and press the "Class Language" button to toggle between the two different language classes.
See Also
Reference
compilation Element (ASP.NET Settings Schema)
Concepts
Shared Code Folders in ASP.NET Web Site Projects
ASP.NET Application Life Cycle Overview for IIS 5.0 and 6.0
ASP.NET Web Site Project Precompilation Overview