Setting Up ASP.NET Core API Environment In Visual Studio 2019

This article will give an overview of how to set up an ASP.NET Web API Core development environment for beginners. In this article I have used Visual Studio 2019 and ASP.Net Core 3.1 version which is the latest stable version. Microsoft has released ASP.Net Core 5.0 but the version is in preview. So I recommend using the stable version for your project.
 
A beginner should have the following understanding.
  • Basics of Visual Studio IDE 2019
  • Basic understanding of ASP.Net core file and folder structure.
  • C# programming
Step 1
 
Open Visual Studio 2019 and select the ASP.NET Core Web Application template and click Next.
 
Setting Up ASP.NET Core API Environment In Visual Studio 2019 
 
Name the project ProductServices and click Create.
 
Setting Up ASP.NET Core API Environment In Visual Studio 2019 
 
In the Create a new ASP.NET Core Web Application dialog, confirm that .NET Core and ASP.NET Core3.1 are selected. Select the API template and click Create.
 
Setting Up ASP.NET Core API Environment In Visual Studio 2019 
Step 2
 
In Solution Explorer, right-click the project. Select Add > New Folder. Name the folder Models. Right-click the Models folder and select Add > Class. Name the class Product, Category and select Add.
  1. using System.ComponentModel.DataAnnotations;  
  2.   
  3. namespace ProductServices.Models  
  4. {  
  5.     public class Product  
  6.     {  
  7.         [Key]  
  8.         public int Id { getset; }  
  9.   
  10.         [StringLength(100)]  
  11.         public string ProductName { getset; }  
  12.   
  13.         public double ProductPrice { getset; }  
  14.   
  15.         public int CategoryId { getset; }  
  16.         public Category Category { getset; }  
  17.   
  18.     }  
  19. }  
  1. using System.ComponentModel.DataAnnotations;  
  2.   
  3. namespace ProductServices.Models  
  4. {  
  5.     public class Category  
  6.     {  
  7.         [Key]  
  8.         public int Id { getset; }  
  9.   
  10.         [StringLength(100)]  
  11.         public string CategoryName { getset; }  
  12.     }  
  13. }   
Step 3
 
Now from the Tools menu, select NuGet Package Manager > Manage NuGet Packages for Solution. Select the Browse tab, from the search box. Search and Select the Project check box in the right pane and then select Installfollowing packages.
  • Microsoft.EntityFrameworkCore.SqlServer
  • Microsoft.EntityFrameworkCore.Tools
Step 4
 
In solution Explorer, right click the project. Select Add > New Folder. Name the folder Data. Right-click the Data folder and select Add > Class. Name the class ApplicationDbContext and select Add. Now inherit ApplicationDbContext from DbContext class which is present in Microsoft.EntityFrameworkCore namespace. 
  1. using Microsoft.EntityFrameworkCore;  
  2. using ProductServices.Models;  
  3.   
  4. namespace ProductServices.Data  
  5. {  
  6.     public class ApplicationDbContext : DbContext  
  7.     {  
  8.         public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options)  
  9.             : base(options)  
  10.         {  
  11.   
  12.         }  
  13.         public DbSet<Product> Products { getset; }  
  14.         public DbSet<Category> Categories { getset; }  
  15.     }  
  16. }  
Step 5
 
Now click on appsettings.json file and “Add” connection setting. In ASP.NET Core the configuration system is very flexible, and the connection string could be stored in appsettings.json, an environment variable, the user secret store, or another configuration source. 
  1. "ConnectionStrings": {  
  2.     "DBCS""server=(localdb)\\MSSQLLocalDb; database=ProductDB; Trusted_Connection=true;"  
  3.   },  
Step 6
 
Now open Startup.csfile in your project. The context is typically configured in Startup.cs with the connection string being read from configuration. The GetConnectionString() method looks for a configuration value whose key is ConnectionStrings:<connection string name>. 
  1. services.AddDbContext<ApplicationDbContext>(options => options.  
  2.             UseSqlServer(Configuration.GetConnectionString("DBCS")));  
Step 7
 
Now from the Tools menu, select NuGet Package Manager > Manage NuGet console. Type command “Add-migration InitialModel” then hit enter and it will generate a folder “Migrations” under solution explore which has a migration file under it. After adding migration type a command update-database and hit enter. The database will be generated and updated in SQL Server Object explorer.
 
Setting Up ASP.NET Core API Environment In Visual Studio 2019
 
Setting Up ASP.NET Core API Environment In Visual Studio 2019
 
Step 8
 
Now from solution explorer right click on project solution, go to property on right panel click on debug, launch browser and replace with api/products and save.
 
Setting Up ASP.NET Core API Environment In Visual Studio 2019
 
Step 9
 
Now it’s time to build and run your project by pressing ctrl+F5.
 

Summery

 
In this I have explained how set up web API core environment for developing core API applications. In the next article I will show you how to add an empty API controller and create CURD operations in API core.