Introduction
Visual Studio Code (VS Code) is a source code editor developed by Microsoft. It is most popular with several developers and educational institutions around the world and it can be used for varieties of programming languages for instance, C, C#, C++, Java, Go, JavaScript, Python, Node.js, rust, and many more. These days VS code is getting more popular. It is a handy IDE for developing applications of any platform as well as it is open source and completely free for any kind of development. We can get several extensions and packages to develop applications in several languages inside it. Additionally, it is lightweight in comparison to Visual Studio.
This article describes the full utilization of Visual Studio code for .NET development. Additionally, the article demonstrates how to create a console app, class library project, solution, and ASP.NET Core 7 application with an example.
This article will cover:
- What are the things needed for .NET development in VS Code
- Getting Started with Console app, class library project, solution creation, and ASP.NET MVC app creation using VS code with sample example.
Prerequisites
- Visual Studio Code
- .NET 7 SDK and C# extension to be installed
Create a Solution using VS Code
Step 1
Open VS code and open the folder where you want to create a project. From the main menu select File-> Open Folder
Step 2
Open the Terminal by selecting View then go to Terminal from the menu or(Ctrl+Shift+P) or simply click on the terminal. Then the terminal opens with the command prompt with the folder name.
Step 3
Run the below command to create a solution in the terminal
dotnet new sln
The solution will be created as shown below with the name of the folder.
Now, we will create a Class Library project.
Create a Class library project
Step 1
In the terminal, you need to run the below command to create a class library project.
dotnet new classlib -o YourClassLibraryprojectname
Below is an example of creating a HelloWorld class library project.
dotnet new classlib -o HelloWorld
It will create a class library project with the name HelloWorld.
Step 2 - Add class lib project to the solution
Run the below command to add the class lib project to the solution.
dotnet sln add YourClassLibraryprojectname/YourClassLibraryprojectname.csproj
dotnet sln add HelloWorld/HelloWorld.csproj
The default Project structure will be like below.
Below is HelloWorld.csproj file.
Create a console App
Step 1
Run the below command.
dotnet new console -o SampleConsole
It will add the console app SampleConsole
Step 2
Add the above console app to the solution
dotnet sln add SampleConsole/SampleConsole.csproj
Now let's run the console app using the below command.
dotnet run --project SampleConsole/SampleConsole.csproj
Create ASP.NET MVC project
Step 1
Open the command prompt or terminal in VS Code. In the terminal run the below command to create a new ASP.NET MVC project.
dotnet new mvc -n SampleDemo
Step 2
Run the below command to add the above application to the solution.
dotnet sln add SampleDemo/SampleDemo.csproj
Below is the default project structure.
Step 3
Go to your ASP.NET Core MVC project directory by running the below command.
cd SampleDemo
Step 4
Let’s run the project using the below command.
dotnet run
The app is running on the localhost port as depicted below.
Let’s open it in the browser using the above localhost port.
If you want you can stop running applications using Ctrl+C.
Adding NuGet Package Manager
To add the NuGet package simply, you can use a command in the terminal like below.
Below is an example to add EntityFramework you can run the below command.
dotnet add package Microsoft.AspNetCore.Diagnostics.EntityFrameworkCore
Alternatively, you can install NuGet Package Manager GUI from https://marketplace.visualstudio.com/items?itemName=aliasadidev.nugetpackagemanagergui
Or install from the extension Or NuGet Gallery.
Conclusion
Hence, in this article, we have learned to use Visual Studio code for .NET 7 development. Additionally created a class library project, a console application, and ASP.NET Core 7 web application as well as explored adding NuGet packages. I hope you find it helpful to use Visual Studio code for .NET application development using the latest .NET framework.