How can I have c# and vb.net code behinds in the same ASP.NET Web Forms application?

638 views Asked by At

I have inherited a vast web forms application that was auto generated from a classic asp site.

This has left .aspx pages with code embedded into them (No separate codebehind) Some attempts have been made to modernise and so some pages exist with C# code behind. These are mixed in the same folders as the pages with vb.net embedded.

I am having a go at getting the codebase into some kind of shape... and my first step was to move embedded code into a code behind... but I cannot as the compiler is expecting codebehind to be in C# and does not recognise the codebehind files I create (neither will Visual Studio combine the files in its GUI).

What steps can I take here? If I must pull c# files into separate folders I can do that, but I am not sure I could get a separate project working, it would change the deploy steps to much (I think)

The aim is to get the logic under some sort of test framework so I can work on re-factoring and safely making changes.

2

There are 2 answers

0
CindyH On BEST ANSWER

A single project must have a single language. (Folders are pretty much irrelevant, except for making sure you keep your namespaces correct.) But I've successfully created a separate C# project for a VB.Net aspx project with terribly intertwined code-in-the-page, as you are describing. The way I did it, that worked quite well, was to

  1. Create a separate C# project with business logic
  2. Call the C# business logic from the VB aspx pages

Each project compiles into its own assembly (dll), and it is easy to call functions in one project from another. I didn't have trouble (that I remember anyway) with the deployment. And if your project is "vast" and contains many folders, it probably should be broken into separate projects anyway. Good luck! :)

0
Shriike On

This is impossible. When the compiler is building your project it's expecting one language. You either need to leave the VB.Net in the aspx (which is interpreted and not compiled), or move the code behind files for those VB.Net pages into another assembly that is referenced in your main app.