Getting Started ASP.NET Core using Visual Studio Code

asp.net core
Microsoft had announced the general availability of .NET Core 2.1 along with ASP.NET Core 2.1 and Entity Framework 2.1 in May 2018. This was announced with LTS (Long Term Support). The detailed blog post was available in .NET blog. I am really excited to try this out with my favorite lightweight x-platform editor Visual Studio Code. In this article, you will see how to create an API in ASP.NET Core 2.1 using Visual Studio Code.
The best way to learn is to build a simple API to perform a basic CRUD operation. It helps me to showcase how to use Visual Studio Code to develop ASP.NET Core application. It is super easy to develop and I am sure you gonna like it!

Download Visual Studio Code and related tools

Install .NET Core SDK is pretty straightforward. You need SDK (which includes runtime) for the development purpose. It is available to download from here. Visual Studio Code is a lightweight and powerful source code editor. It runs on your desktop and is available for Windows, mac OS and Linux. It supports most of the languages using extensions. You can download the tool from here. Once both the tools are downloaded, you can open Visual Studio Code and install following two extensions from the marketplace

Create a solution containing API project

Once the development tools are ready to use, you will set up the solution containing a ASP.NET Core Web API project. Please follow the steps below in order

  • Create a folder named “eKart” and open the folder in Visual Studio Code
  • Open Visual Studio Code and click on terminal [Ctrl + `]
  • Fire up the below commands in the order mentioned below

Prepare the API Project for Database Connectivity

The objective is to build the API using ASP.NET Core to perform CRUD operation and store the data in SQL database. Undoubtedly Entity Framework Core is the first class citizen as an ORM tool. This requires following nuget packages to add to the ASP.NET Core API project. It includes all the required packages (a feature enhancement called metapackage)

  • Microsoft.AspNetCore.App

Perhaps the easiest way to add packages in Visual Studio Code is to use shortcut key [Ctrl + Shift + P] to open the command palette and type “nuget”. The following dialog appears and you can follow on-screen guidance to add the above packages. Once added, the package information is added to the csproj file.vs-code-nuget-mgr

Create the Model and DataSource Context

We will begin with a POCO (Plain Old CLR Object) class Product to map the Product entity.

Here a data context is required to have a two-way communication between the Product entity and the data source using Entity Framework Core. Apparently an interface is used to abstract out the implementation between SQL Server data context and in-memory data context and inject the specific implementation later.

Based on the above interface, the SQL Server implementation is derived likewise

Perform Entity Framework Core Migration

Entity Framework Core supports code-first approach to generate database objects. The database model in C# code is written. Next step is to create the database table using the model. This requires an entity framework migration to perform. Before doing the migration, lets set up the connection string of the database in Startup.cs

Finally we can initiate the entity framework core migration to do the changes in the database. The following command runs inside the eKart.API directory. This ensures the database eKart is created.

Create an API Controller

The objective of API Controller is to expose GET and POST endpoints to perform basic CRUD operation.

Configure Swagger to test the API

Once you run the api project, you cannot test it using browser. Either you have to use Fiddler or Postman to call the api endpoints to perform the test. There is an easier way to list down the available api endpoints using Swagger. Swagger is very usefule tool while generating the documentation about api. There are multiple Swagger implementation for .NET like Swashbuckle and NSwag. I have used Swashbuckle in this article. To use Swashbukle, you need to add the nuget package

Run the API

Finally we are ready to run the API project. You can type the following commands to run the API

The API can be accessed from https://localhost:5001/swagger/index.html. The GitHub repository is located here