Partager via


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

  1. Open Visual Studio.

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

  3. Under Installed Templates, click the language that you prefer to work in, and then 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 project.

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

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

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

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

    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 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;
          }
        }
    }
    
  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 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

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

  2. If your project 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 project can use classes in both programming languages.

To see the results of using multiple programming languages

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

    1. In Solution Explorer, right-click the Web site project 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 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

Other Resources

ASP.NET Web Projects

ASP.NET Web Site Projects