Introduction:
Microsoft Visual Studio Code is a lightweight editor that supports most of the programming languages. Dotnet Core can be run and debug using Visual Studio Code as we do in Visual Studio editor which is the recommended and famous editor for Dotnet Applications.
Before starting anything, install the latest version of Dotnet Core SDK from the Microsoft DotnetCore website and also download the Visual Studio Code editor which is the free editor.
C# for Visual Studio Code Extensions:
In Visual Studio Code, we can get a lot of extensions plugins which makes development more easy and fast. Similarly to work with C# we need to install the VS Code extension of "C# for Visual Studio Code". Few features of this extension like:
- .NET Core lightweight development tool.
- Good C# editing support.
- Syntax Highlighting.
- Good Code Intellisense.
- Nice debugging support.
Create A Dotnet Core Solution:
In Visual Studio solution file automatically get created while creating the .NET Core application, but in Visual Studio Code, we need to create the solution file. It is not mandatory to have a solution file in .Net Core application, but it is an individual choice of having it like a parent of all .Net Core projects. ".sln" is the file extension for the solution file.
.NET Core CLI Commands we can generate a solution file using either Windows Command Prompt or Visual Studio Code Terminal (Powerfull command prompt comes with Visual Studio Code). The following command to create the ".sln" file.
dotnet sln new
To start creating a project let's create a root folder and I'm naming the folder as "TestCore" and then open the visual studio code and go to the "File" menu and select on the "Open Folder" option, then choose our folder to created previously.
Now go to the "View" menu and select on the "Terminal" option, then enter the above CLI command on the Terminal as below.
Now after executing the command ".sln" file gets created with the name of the root folder as below.
Now go to the "View" menu and select on the "Terminal" option, then enter the above CLI command on the Terminal as below.
Now after executing the command ".sln" file gets created with the name of the root folder as below.
.NET Core Application Templates:
.Net core provided all templated options to create applications like MVC, Web API, Blazor, etc. Few of the CLI commands to create projects with their respective templates as follows:
CLI command to create .Net Core Console Application
dotnet new console -n Your_Application_Name
CLI command to create .Net Core MVC Application
Let's test our setup by creating MVC template application as below:
dontet new mvc -n Your_Application_NameCLI command to create .Net Core Class Library
dotnet new classlib -n Your_Application_NameSo to check all types of templates supported by .NET Core run the following command
dotnet new -lOn running the command it will show different templates as below:
Let's test our setup by creating MVC template application as below:
Attach Project To Solution File:
Run the following command to add c# project to the solution file
dotnet sln Your_Solution_FileName.sln add Your_Project_Folder\Your_Project_Name.csproj
Add Project Reference:
Let's create a .NET Core ClassLibrary project by running the following command
dotnet new classlib -n Your_ApplicationName
Now let's add ClassLibrary project to the solution file as below
Now I'm going to add this ClassLibrary project reference into the MVC project. Adding a reference to the project can be done with the following command.
dotnet add Application_Expecting_Reference_Folder/Application_Expecting_Reference_Project reference Refered_Application_Folder/Refered_Application_ProjectNow we can confirm the reference presence by opening the project as below.
Add Build And Debug Configuration Files:
Creating projects in the Visual Studio Code will show a notification to install build tools as below.
On selecting the 'Yes' button, we can observe a couple of JSON files with build settings as below.
On selecting the 'Yes' button, we can observe a couple of JSON files with build settings as below.
Build And Run Project:
Building the project at the solution level will help in building all the projects that are added to the solution file. The command to build the project as below.
dotnet build
Now go to the folder of an application be run and enter the following command to run the application
dotnet run
Debugging From Visual Studio Code:
Visual Studio Code editor we can find the "Debug" options in menus as below
From Debug menu select option like "Start Debugging" and then the application will get run as below
Wrapping Up:
Hopefully, this article will help to create, run and build the .NET Core application in Visual Studio Code. I will love to have your feedback, suggestions and better techniques in the comment section.
Comments
Post a Comment