CodeSnip: One Application, Several Languages
page 1 of 1
Published: 12 Dec 2005
Unedited - Community Contributed
Abstract
In this code snippet, Bilal Haidar will demonstrate how we can have an ASP.NET 2.0, VB.NET Web application with its own C# and VB.NET classes placed in the App_Code directory.
by Bilal Haidar
Feedback
Average Rating: 
Views (Total / Last 10 Days): 10673/ 15

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.



User Comments

Title: 2012 NFL jerseys   
Name: NIKE NFL jerseys
Date: 2012-07-02 10:08:32 AM
Comment:
http://www.jersey2shop.com
http://www.cheapjersey2store.com
http://www.jerseycaptain.com
http://www.yourjerseyhome.com
We are professional jerseys manufacturer from china,wholesal.cheap nike nfl jerseys, mlb jerseys, nhl jerseys,nba jerseys and shoes
Cheap NFL,NBA,MLB,NHL
,heap jerseys,2012 nike nfl Jerseys,nba jersey and shorts,oklahoma city thunder jersey,official jeremy lin new york knicks jersey,NFL Jerseys Wholesale,blake griffin jersey blue,NFL jerseys For Sale online.All Our Jerseys Are Sewn On and Directly From Chinese Jerseys Factory
,Wholesale cheap jerseys,Cheap mlb jerseys,]Nike NFL Jerseys,Cheap China Wholesae,Wholesale jerseys From China,2012 nike nfl Jerseys,Jerseys From China,,2012 nike nfl Jerseys,Revolution 30 nba jerseys,jersey of nba chicago bulls direk rose ,nfl jerseys,green bay packers jerseys wholesale,Buffalo Bills nike nfl jerseys sale,good supplier soccer jerseys,cool base mlb jerseys,Revolution 30 nba jerseys,2012 stanley cup nhl jersey,
We are professional jerseys manufacturer from china,wholesal.cheap nike nfl jerseys, mlb jerseys, nhl jerseys,nba jerseys and shoes. www.yourjerseyhome.com
Title: ASP learning   
Name: SQL course
Date: 2011-01-11 10:35:10 PM
Comment:
I will try this one. I find this interesting.
Title: Application/session access within the app_code   
Name: Genious
Date: 2008-12-10 7:07:53 AM
Comment:
thnaks that is quite helpfull.

my question is how we can access the Applicaion/session objects with in the app_code class.

thanks
Title: Trouble importing class from DLL file   
Name: antikpat
Date: 2008-04-08 4:10:20 PM
Comment:
I have a DLL file in my Bin folder and I have to use a class from this DLL in a .vb file that I have stored in the VB folder. "Import DLLNamespace" just does not work. Does anybody know how to make this work?

Tks!
Title: Engineer   
Name: rockSRQ
Date: 2006-05-24 3:38:52 PM
Comment:
Excellent -- this saved me a ton of work and took about 90 seconds to accomplish.
Title: Re: Adam   
Name: Bilal Hadiar [MVP]
Date: 2006-04-27 1:05:39 AM
Comment:
Hi:
Well the article did list well how to manage your classes inside your application:
First of all we said that we created a new VB.NET web application. Then:
"we add a new object written in Visual Basic .NET to the"

Then:
"In this step, we create a new directory, called CS, inside the App_Code directory"

So I believe any anyone with "minimum" common sense would know that, the VB.NET objects will go in the App_Code directly. And the C# object would go into the CS directory under App_Code.

Thanks.


App_Code directory
Title: Developer   
Name: Adam
Date: 2006-04-26 11:10:30 AM
Comment:
In case anyone else runs into the same problem, you want to make sure that you put the C# code in the CS directiory under App_Code and put the VB code in thr VB directory under App_Code. The tutorial above doesn't explcitly tell you to put the VB files in the VB directory.
Title: Thanks   
Name: Muddassar
Date: 2006-04-06 7:12:42 AM
Comment:
Thanks a lot. Saved a lot of pain
Title: Re:   
Name: Bilal Haidar [MVP]
Date: 2006-01-02 2:29:08 PM
Comment:
Thank you so much for your precious comment.

Regards
Title: On Behalf of MKamoski@WebLogicArts.com   
Name: MKamoski@WebLogicArts.com
Date: 2006-01-02 2:27:47 PM
Comment:
For some strange reason, it is not possible for me to post a comment in the comment-thread of your article at http://aspalliance.com/745 because the page keeps crashing and the browser actually closes. I tried several times. Who knows, maybe it is the corporate firewall here. Anyway, I wanted to shared the following; so, I am sending it direct. If you want to post it as an "FYI", that would be fine with me. I just thought you would want to know. Thank you. The comment is: "Your article on mixing VB.NET and C#.NET is a nice resource. However, I would like to point out that pages built in VB.NET and C#.NET page can be mixed in a Framework 1.1 ASP.NET application. Basically, the trick is to use the "Src" attribute in the "@Page" directive. It is not nearly as nice as doing it in Framework 2.0; but, it can be done and does work. If you are interested, go to http://www.WebLogicArts.com/DemoVBInCSProject01.aspx to view the complete code listing and a working sample. Regardless, thank you for a great article.
Title: Re:   
Name: Bilal Haidar [MVP]
Date: 2005-12-25 3:04:00 PM
Comment:
What was the error generated? I worked it here and it functions well.

Regards
Title: DL   
Name: PP
Date: 2005-12-25 2:45:36 PM
Comment:
If you could provide all the above steps for an C# application, that would have been great help. I tried these steps on a project of C#, and it failed on Step-6.
Title: Dev Lead   
Name: PP
Date: 2005-12-25 2:01:30 PM
Comment:
Adding a screen shot of Solution Explorer before/after adding the VB & CS folder+ file will help explain the things much better.
The text/note used to explain the concept was clear, but took little extra time to understand the concept :)






Community Advice: ASP | SQL | XML | Regular Expressions | Windows


©Copyright 1998-2024 ASPAlliance.com  |  Page Processed at 2024-03-29 1:01:20 AM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search