Most .NET development shops either develop in C# or Visual Basic (VB). However, depending on who you talk to, C# is probably the most preferred server-side programming language in most of these shops. Personally, I prefer C#; but as a Freelance Consultant, it is in my own interest that I stay unbiased in my judgment of programming languages, and rightfully so.
It is important to note that the source code in the “App_Code” folder of a Web Application Project is compiled into a single assembly. Hence, all the files found directly in the “App_Code” folder must be in the same programming language. The truth is, you can develop a web project solution using both languages if you really need to. So let’s say you purchase a 3rd-party application developed in VB.NET (eg. a Content Management System like CommunityServer or Ektron CMS400.NET), but you prefer to develop your custom code/classes using C#, you can go about implementing it as follows.
You will want to create Sub-folders under the ‘App_Code” folder (eg. “VBCode” for your VB classes and “CSCode” for your C# classes) so ASP.NET can treat the sub-folders as separate compile-able units. In other words, the C# compiler will be used to compile your C# code and the VB compiler, for the VB code. The setup doesn’t end there — you will have to create a codeSubDirectories element in the compilation element of the Web.Config file and subsequently, add a reference of the sub-folders like so:
<configuration>
...
<system.web>
...
<compilation debug="false">
<codeSubDirectories>
<add directoryName="VBCode" />
<add directoryName="CSCode" />
</codeSubDirectories>
</compilation>
...
</system.web>
...
</configuration>
At compile time, ASP.NET infers the right compiler to use in each sub-folder based on the files in the sub-folder.
This is not necessarily a new concept; however the sad part is, if you have an Application which inherits your ‘parent’ site’s Web.Config file, you might have to ‘remove’ or ‘clear’ the codeSubDirectories element from the ‘child’ (or inheriting) Web.Config file to prevent the .NET error “Parser Error Message: The code subdirectory ‘/App_Code/VBCode/’ does not exist.”
The norm is, typically, to specify in the child Web.Config file, something similar to the following:
<codeSubDirectories>
<remove directoryName="VBCode" />
<remove directoryName="CSCode" />
</codeSubDirectories>
However, ASP.NET throws the following error “Parser Error Message: Unrecognized element ‘remove’.” if you try to implement the ‘remove’ element. In the same vein, a similar error is raised when you try to use the ‘clear’ element, even though the MSDN documentation insists that this syntax is correct. Unfortunately, this bug is present in all .NET Frameworks, thus far (including the 3.5 framework).
The Workaround?
You will have to create corresponding sub-directories in the child application’s “App_Code” folder for “VBCode” and “CSCode”.