AspAlliance.com LogoASPAlliance: Articles, reviews, and samples for .NET Developers
URL:
http://aspalliance.com/articleViewer.aspx?aId=745&pId=-1
CodeSnip: One Application, Several Languages
page
by Bilal Haidar
Feedback
Average Rating: 
Views (Total / Last 10 Days): 10628/ 10

Introduction

ASP.NET 2.0 introduces the App_Code directory, which is added by default to every new Web application created using Visual Studio.NET 2005.

This folder can be used to place standalone code files that are to be shared among all the pages inside a single Web application. At run-time, this folder will be compiled and a single assembly is created and made available to all the pages in the application.

A nice feature about this folder is that we can place code from several .NET languages. In other words, we can have objects written in C# and objects written in VB.NET and even other languages that can be used with ASP.NET development.

In this code snippet, we will demonstrate how we can have an ASP.NET 2.0/VB.NET Web application with its own C# and VB.NET objects placed in the App_Code directory.

Demonstration

In this step-by-step demonstration, we will create a new Web application and show you how to set up your application to handle objects from both C# and VB.NET languages.

Step1:

The first thing to do is to create a new ASP.NET website with Visual Basic as the main language in that application.

Step2:

Open the Web application configuration file and add the configuration lines shown in listing 1 below to the system.web section.

Listing 1: Configuring Web.config

<configuration>

  <system.web>

    <compilation>

      <codeSubDirectories>

        <add directoryName="VB"/>

      </codeSubDirectories>

    </compilation>

  </system.web>

</configuration>

Step 3:

In this step, we add a new object written in Visual Basic .NET to the App_Code directory. The object will contain only one single function, which is shown in listing 2 below.

Listing 2: VB.NET Object Placed in App_Code

Imports Microsoft.VisualBasic

Public Class VBObject

  Public Function FormatString(ByVal inputStr As String) As String

    Return "Accessed from Vb.NET object " & inputStr

  End Function

End Class

Listing 2 presents a function that takes as input a parameter of type string and returns a string stating the object language from which the function is being called. Notice when you try to create a new object for the first time, Visual Studio.Net will ask you if you want to create an App_Code directory and place the object inside it.

Step 4:

In this step, we create a new directory, called CS, inside the App_Code directory. If you look at the Web.config configuration section where we add a sub-directory, you will see that the folder to be added should have the name of CS, though in fact, it could be named something else.

Step 5:

Its now time to add a new object written in C#. The new object will be added to the CS sub- directory inside the App_Code directory. The object is shown below in listing 3:

Listing 3: C# Oject Placed in CS Directory Inside App_Code Directory

using System;

public class CSharpObject

{

  public String FormatString(String inputStr)

  {

    return "Accessed from C# object " + inputStr;

  }

}

Step 6:

In this final step, we will add a new ASP.NET page. This page will be relatively simple; we will add the code inside the Page_Load method of this page. The code instantiates the two objects we created before, and then calls each function in those objects to show you how we can use two objects written in two different languages in the same Web application. The code-behind is shown in listing 4 below:

Listing 4: ASP.NET Code-Behind

Partial Class _Default

  Inherits System.Web.UI.Page

  Protected Sub Page_Load(ByVal sender As Object, _
    ByVal e As System.EventArgs) Handles Me.Load

    Dim VBObject1 As New VBObject()

    Response.Write(VBObject1.FormatString("Bilal Haidar"))

    Response.Write("<br />")

    Dim CSharpObject1 As New CSharpObject()

    Response.Write(CSharpObject1.FormatString("Bilal Haidar"))

  End Sub

End Class

Conclusion

As you can clearly see, using objects from different languages is no longer a problem in ASP.NET 2.0. I hope that you find this code snippet useful and start using several .NET languages inside your Web applications when you find it useful.



©Copyright 1998-2024 ASPAlliance.com  |  Page Processed at 2024-04-26 7:44:28 AM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search