public knowledge blog_load(sender Virendra)

                                                                                                                          Nothing shocks me, I am a Software Engineer.

Using C# and VB.NET classes together in the App_Code folder

Posted by Virendra Dugar on June 15, 2009

Download Source Code

Learn new ASP.NET Core 1.0 Framework

In ASP.NET 2.0 one can mix web forms coded in C# and VB.NET together in a single web site. This works great for web forms. However if you want to code classes from App_Code folder in different languages? Such mixing of classes coded in different languages is not allowed with default settings. You can, however,configure your web site to get this done. This article is going to explain you that how one can do that.

Creating sub folders and classes

First of all, Create a new webSite in VS.NET 2005. Add App_Code folder to it. You can do so easily by right clicking on the web site in the Solution Explorer and choosing Add ASP.NET Folder” option (see below).

Different Language

Once you add the App_Code folder add two class files – Class1.cs and Class2.vb. Note that one class file must be in C# where as the other must be in VB.NET. Add the following code in Class1.cs.
C# Code

Similarly, add the following code in Class2.vb.

VB.NET Code

Both of these classes contain a method called HelloWorld() that simply return a string to the caller.

Now try compiling the web site. What happens? You will get an error as shown below:

Error 1 The files ‘/VBandCSharptogether/App_Code/Class2.vb’  and ‘/VBandCSharptogether/App_Code/Class1.cs’ use a
different language, which is not allowed since they need to be compiled together.

The error message bluntly tells us that you can not use different coding languages for the classes in App_Code folder. Fortunately, there is a way to get out of this trap. Firstly you need to put C# and VB.NET classes in separate sub-folders under App_Code. Secondly you need to add some markup in the web.config file to tell ASP.NET compiler about your intention.

<codeSubDirectories> Section

Create two folders under App_Code filder named CSCode and VBCode. Move Class1.cs inside CSCode folder and Class2.vb inside VBCode folder.

Add a web.config file to your web site and add the following markup to it:

Web Config Code

Here, we added <compilation> section. The <codeSubDirectories> section defines a set of sub-directories relative to App_Code folder that are compiled at run time. The directoryName attribute points to the sub-folder of App_code. Each sub folder is compiled separately and hence each can have classes coded in different languages. We added our CSCode and VBCode folder in this section. This way the compiler will compile classes from CSCode and VBCode folders separately.

After configuring the web site try to compile it. This time it compiles successfully.

Enjoy….

Quick summary of what’s changed in ASP.NET Core 1.0

18 Responses to “Using C# and VB.NET classes together in the App_Code folder”

  1. […] some VB code and it was too hard to try and wrap my mind around. I quickly set up the app to compile both VB and C# classes so I could keep the legacy while I develop new stuff and haven’t looked back […]

  2. Tehmina said

    Very nice blog header … blog_load(sender virendra) 🙂

  3. lancespurgeon said

    Hey Guys somehow this will just not work… is it to do with the fact that i am using .net 3.5

    Configuration Error
    Description: An error occurred during the processing of a configuration file required to service this request. Please review the specific error details below and modify your configuration file appropriately.

    Parser Error Message: Unrecognized element.

    Source Error:

    Line 153:
    Line 154:
    Line 155:
    Line 156:
    Line 157:

    Source File: C:\inetpub\wwwroot\1.fruithosting.com\web.config Line: 155

    Version Information: Microsoft .NET Framework Version:2.0.50727.4016; ASP.NET Version:2.0.50727.4016

    I have been busy with this issue almost a day now … !!!!

  4. lancespurgeon said

    I left out the part what causes the error:

  5. Tnelson said

    There’s good info here. I did a search on the topic and found most people will agree with your blog. Keep up the good work mate!

  6. RobD said

    Hey, I found your blog while searching on Google your post looks very interesting for me. I will add a backlink and bookmark your site. Keep up the good work! 🙂

  7. Hey, I read a lot of blogs on a daily basis and for the most part, people lack substance but, I just wanted to make a quick comment to say GREAT blog!…..I”ll be checking in on a regularly now….Keep up the good work! 🙂

  8. Argentieri said

    Thanks, that really helped =)

  9. NoMan'sLand said

    Bloody awsome

  10. Senthil said

    Wow, that was Easy. Thanks for Sharing.

  11. jcnet said

    Thanks – that worked. Nice having options.

  12. Guru Masterclass…

    Using C# and VB.NET classes together in the App_Code folder « public knowledge blog_load(sender Virendra)…

  13. Gino said

    Saved me many hours! Thanks so much for sharing!

  14. Thanks! 🙂

  15. but if i have C# dll and want to use in asp.net codebehind vb.net then how to use it

  16. Raja Dashnamoorthy said

    i am using vs2010 even i done all the things you told me still i am getting same error

  17. Very good, thanks.

Leave a comment