Using VS Code with a Legacy .NET Project

Use visual studio code with an older C# WebForms, ASP.NET, or MVC solution


I think one of the worst parts about working with a “legacy” codebase is often times the tooling. For years any .NET project had a tight ball-and-chain relationship with Visual Studio, SSMS, and many other multi-gigabyte Microsoft tooling dependencies.

Wanting to modernize, Microsoft open-sourced a new light-weight cross-platform version of .NET called dotnet core. In tandem they have also released a fantastic minimal editor called Visual Studio Code, or VS Code for short.

Unfortunately, most VS Code tutorials center around the newer dotnet-core language, and not on older versions of .NET and VB. In this post I will walk you through how to use VS Code with an existing project. I am assuming that you already have the project working and building in Visual Studio, which is still unavoidable to ensure you have the correct tooling installed on your machine.

1. Install VS Code

Head over to https://code.visualstudio.com/and download VS Code for your operating system. Have fun customizing and installing themes, and getting a feel for the editor. I'm assuming you are already a fan of VS Code if you are reading this post.

2. Install C# Extension

Install the C# extension by Microsoft.. This extension provides some basic intellisense and syntactical features. If you find it chugging along a little slow on bigger projects, you can disable some of the code linking/reference features by turning off Code Lens in the VS Code user settings (which are stored as JSON) with this setting: "editor.codeLens": false.

3. Open Your Project Directory with VS Code

This can be done from the windows explorer context menu, or from with VS Code under File > Open Folder. Note that this is different from Visual Studio... you do not want to try and open the .sln file. Most likely this will be the directory that is under version control, or the directory containing your .sln file.

On the left hand file explorer you should see all of the files in a tree similar to that normally seen on the right hand side of Visual Studio.

4. Build Your Solution

Following step 3, you are ready to code. But what about compiling your solution? In Visual Studio we have various build profiles that execute many different steps and custom scripts. Even the most basic project will still have a Debug and Release profiles by default. One of these must be specified when compiling.

To compile your solution, use the command line. VS Code has a nice built in command line that can be shown or hidden by typing Ctrl + ~ in the editor. By default on Windows this will open up power shell, but an be changed to command prompt in the settings.

Make sure the command prompt is in the directory containing the .sln file. To build your project using a custom profile, type:

If using Command Prompt
"C:\Program Files (x86)\MSBuild\14.0\Bin\MSBuild.exe" /P:Configuration=Debug
If using Power Shell
& "C:\Program Files (x86)\MSBuild\14.0\Bin\MSBuild.exe" /P:Configuration=Debug

Replacing Debug with whatever your custom build profile is. NOTE that this assumes you are using version 14.0 of the msbuild tool. You will want to hunt down MSBuild.exe on your system if using an older version of Visual Studio.



Tips and Tricks

Here are a few of my favorite tips that might aid in use of VS Code when working with large projects.

Ctrl + e and then start typing to quickly switch between files. It's a huge time-saver to avoid navigating the giant file tree, or having to hunt and peck for an open tab.

Add "C:\Program Files (x86)\MSBuild\14.0\Bin" to your PATH to simply type msbuild /P:Configuration=Debug in the command line to make a build.

Use the built-in git diff tool to quickly see what you have changed. Click the "source control" icon in the left toolbar, and then double click on a file in the sidebar to view the changes