Walkthrough: Develop Web Sites Using Multiple Programming Languages

By default, the App_Code folder does not allow multiple programming languages. However, you can modify your Web application 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 Sites. 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 the how to add multiple programming languages to an ASP.NET application.

Creating the Web Site

If you have already created a Web site in Visual Web Developer by completing Walkthrough: Creating a Basic Web Page in Visual Web Developer, you can use that Web site and go to the next section. Otherwise, create a new Web site and page by following these steps.

To create a file system Web site

  1. Open Visual Web Developer.

  2. On the File menu, click New, and then click Web Site. If you are using Visual Web Developer Express, on the File menu, click NewWeb Site.

    The New Web Site dialog box appears.

  3. Under Visual Studio installed templates, click ASP.NET Web Site.

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

    For example, type the folder name C:\WebSites\BulkUpdate.

  5. In the Language list, click the programming language you prefer to work in.

  6. Click OK.

    Visual Web Developer creates the folder and 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

  1. If your Web site does not already have an App_Code folder, do the following:

    1. In Solution Explorer, select the name of the Web site.

    2. In the Website menu, click Add ASP.NET Folder, and then click App_Code.

  2. In Solution Explorer, right-click the App_Code folder and then click New Folder.

  3. Name the new folder "CSCode".

  4. Select the CSCode folder.

  5. In the Website menu, click Add New Item.

    The Add New Item dialog box is displayed.

  6. In the Add New Item dialog box, choose the Class template, name the class "CSExample", select C# as the language, and click Add.

  7. In Solution Explorer double-click the CSExample.cs file to open it.

  8. Add the following code to the CSExample.cs file, overwriting the existing CSExample class already in the file:

    public class CSExample
    {
        private string teamString;
        public CSExample()
        {
            TeamString = "C# Code";
        }
        public string TeamString 
        {
          get {
            return teamString;
          }
          set {
            teamString = value;
          }
        }
    }
    
  9. 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.

  10. Add the following code to the VBExample.vb file, overwriting the existing VBExample class 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 configuration so that ASP.NET will compile the subfolders separately.

To modify the Web.config file to support multiple programming languages

  1. In Solution Explorer, select the name of the Web site.

  2. If your Web site does not already have a Web.config file, do the following:

    1. In the Website menu, click Add New Item.

    2. Choose Web Configuration File and then click Add.

  3. Double-click the Web.config file to open it.

  4. 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 can use classes in both programming languages.

To see the results of using multiple programming languages

  1. If your Web site does not already have a Default.aspx page, do the following:

    1. In Solution Explorer, right-click the Web site name and then click Add New Item.

    2. Select Web Form, name the page "Default.aspx" and then click Add.

  2. In Solution Explorer double-click the Default.aspx page.

  3. Add a Button control to the Default.aspx page.

  4. Set the Button control's text to "Class Language" and the ID of the button to "classLanguage".

  5. Add a Label control to the Default.aspx page, set its ID property to "classLabel", and clear its Text property.

  6. In Design view, double-click the Button control to create an event handler for its Click event.

  7. 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
    
  8. In Solution Explorer, right-click Default.aspx and select Set As Start Page.

  9. Run the Web site and press the "Class Language" button to toggle between the two different language classes.

See Also

Concepts

Shared Code Folders in ASP.NET Web Sites

ASP.NET Application Life Cycle Overview for IIS 5.0 and 6.0

ASP.NET Precompilation Overview

Reference

compilation Element (ASP.NET Settings Schema)