The best way to learn is to build a simple API to perform a basic CRUD operation and test it using Swagger. 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
- C# for Visual Studio code – It helps to write code in C#
- Nuget Package Manager – It helps to manage nuget packages
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)
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.
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 from IDatasourceContext
Perform Entity Framework Core Migration
Entity Framework Core supports code-first approach to generate database objects for Product entity. Next step is to create the database table using the entity. This requires an entity framework migration to perform. Before doing the migration, lets set up the connection string of the database in Startup.csFinally 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(certain browsers doesn’t support). Either you have to use Fiddler/Postman/other REST API tools 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 useful tool for 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 following nuget packageAdditionally you add the swagger generator, swagger middleware and swagger UI to enable the swagger endpoint of the REST api.
Run the API
Finally we are ready to run the API project. You can type the following commands to run the APIThe API can be accessed from https://localhost:5001/swagger/index.html. The GitHub repository is located here