Sunday, August 27, 2006

 
Compiling VB and C# in ASP.Net 2.0

Anyone who ever had to integrate both C# and Vb.Net code in ASP.Net 1.0/1.1 knows how awkward and time-comsuming it could get sometimes. As much as I love the command prompt and both vbc and csc offer the easiness of just passing in files making it a breeze to do try and errors, it was never much fun guessing in which dll each control was defined.

No worries though for those days are gone! ASP.Net 2.0 makes it really easy to work with mixed code.

The only limitation of working with mixed code is that the files cannot be thrown together on the App_Code folder. You have to separate them into different folders. I think this is actually a good thing cuz it encourages good organization practices.

So, in your App_Code folder, create one folder called C# and another called VB (you can name it anything you want actually). Now, put all code files in their respective folder according to the language you used to program them.

Now all you have left to do is open your Web.Config file (or create one at the root of your web app if you don't have one in your project yet) and do some (very simple) XML editing.

If you let Visual Studio create your Web.Config root file for you then you should already have a section. If not you'll need to create one. This is where you can specify folders that need to be compiled separately. You don't even need to specify the language to use for different folders, the compiler is smart enough to figure it out himself. So, all you need is to use the <codesubdirectories> element and add entries for the VB and the C# folders like this:



<compilation>
<codesubdirectories>
<add directoryname="VB">
<add directoryname="C#">
</codesubdirectories>
</compilation>

Note that the default Web.Config created by Visual Studio already contains a compilation section so just need to change it. Also it probably has an attribute in the compilation element of debug="true" which is irrelevant here and that's why it wasn't included in the code above.

Oh and don't forget that in ASP.Net you can always have as many Vb.Net and C# web forms as you fancy all mixed in without needing to do a thing.

Now that is a big step towards .Net's self-imposed objective of language independance!

Comments: Post a Comment



<< Home

This page is powered by Blogger. Isn't yours?