Wednesday, August 14, 2019

Creating a PUT (Update) method in Asp.Net Core WebAPI Controller

In this post we will create a HttpPut method in Asp.Net Core WebAPI, which will be used to update a row in the SQL Server database using Entity Framework Core. We have seen how to setup Entity Framework Core in an Asp.Net Core Web API project in the previous posts, in this post we will jump into creating the HttpPut method.

Open the Country controller which we used in the previous post and add the following Update method which uses the HttpPut verb.

Tuesday, August 13, 2019

Creating a POST (Insert) method in Asp.Net Core API Controller

In the previous post we created an API Controller and created a GET method, in this post we will create a POST method which will use Entity Framework Core to insert values to the database. We will use the same country controller and add a Create method.

Open the CountryController and add the following Create method, the method will take an object of type Country and insert the same to the Database.

Saturday, August 10, 2019

Asp.Net Core API Controller with a GET Method

In the previous post we added Entity Framework Core context to the Web API project, in this post we will use the EF context to get a list of values from the DB and expose the data in JSON format using a HttpGet method.

To start with we will have to add a reference of the DataAccess project which we created in the previous post to the WebAPI project. All database operations will be performed in the DataAccess project hence we will add a reference to this project.

Next in the WebAPI project we will add a new API Controller, name it CountryController

Adding Entity Framework Core to the WebAPI Project

In the previous post we saw on how to create a basic WebAPI project in Asp.Net Core using Visua Studio 2017. In this post we shall see on how to add Entitiy Framework Core support to the basic project. First let us create a class Library project with name DataAccess. This project will deal with all database interactions and we will implement EntityFramework Core in this project.

Once the DataAccess project is created, delete the default class1.cs which got created with the project. Add the following NuGet packages using the NuGet package Manager.