29 August, 2011

Using Multiple Programming Languages in a Asp.net Web Site

  • The App_Code folder does not allow multiple programming languages. 
  • still 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. 
Follow the simple steps to do so :
  1.  create simple website project.
  2.  In the Website menu, click Add ASP.NET Folder, and then click App_Code
  3.  Right-click the App_Code folder and then click New Folder, name the new folder "CSCode".
  4.  In "CSCode" folder, Add new item, choose the Class template, name the class "CSExample", select C# as the language, and click Add.
  5. In Solution Explorer double-click the CSExample.cs file to open it.
  6. Add the following code to the CSExample.cs file, overwriting the existing CSExample code that is already in the file. 
CSExample.cs
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 3-6 using the following values:
  • New folder: VBCode
  • New class file: VBExample

VBExample.vb
public class CSExample
{
   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
}

Now the main part is 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:

  
  

Testing the classes

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

No comments:

Post a Comment