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.
- 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
Running the App in the console.
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
The default project structure is given below.
Step 3– Go to your ASP.NET Core MVC project directory by running the below command.
Step 4– Let’s run the project using the below command.
The app is running on the localhost port as depicted below.
Let’s open it in the browser.
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.
The below command installs the Stratis.SmartContracts.NET6 package from NuGet store.
dotnet add HelloWorld package Stratis.SmartContracts.NET6
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
Below is an example to add EntityFramework you can run the below command.
dotnet add package Microsoft.AspNetCore.Diagnostics.EntityFrameworkCore
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 learned to add NuGet packages. I hope you find it helpful to use Visual Studio code for .NET application development using the latest .NET framework.