Create Web API with ASP.NET Core 6.0
Tags: webapi, dotnet, crud
Published: 2022-03-21 17:46:47

Web API is an application programming interface (API) that is used to enable communication or interaction with software components with each other.

Web API is the enhanced form of the web application to provide services on different devices like laptop, mobile, and others.

The Web API component of the application acts as an HTTP based interface which accepts HTTP requests from a third party client application and performs data operations. The following diagram explain the possible architecture of this hypothetical application using Web API.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/05c7kxq4ri46fm2flvks.png)

This application can expose its business logic to the various client applications using Web API. Web API can effectively use the repositories to decouple the Business and Data Access Layers from the Web API Controller class. The Web API application can use inbuilt Dependency Injection (DI) to inject required dependencies in objects. For e.g. The Data Access can be registered in the DI and can be injected in the Business Layer, further the Business Layer can be registered in DI and injected in the Repository, and Repository in the Web API.

- **Principles of REST API**

The six principles of REST API are:

1. Stateless
2. Client-Server
3. Uniform Interface
4. Cacheable
5. Layered System
6. Code on demand

we can create a Web API project in two ways:

1. Web API with MVC Project
2. Stand-alone Web API Project

**Entity Framework core(EF core)** is an ORM library developed by Microsoft for .NET framework. It helps developers conveniently work with database objects without having need to write SQL scripts.

In this tutorial we shall look into create a Stand-alone Web API project configuring EF Core on a dot net core application and how a table can be created and data operations are performed.
Entity Framework of the .NET comes with two approaches: 
1. Code-First approach: Focus on the domain of your application and start creating classes for your domain entity rather than design your database first.
2. Database-First approach: Database and the related tables are created first and then create an entity data models using database.

we are going to use Code-First approach for our project.

## Preparation

1. Install .NET 6 SDK: (https://dotnet.microsoft.com/download/dotnet/6.0)
2. Install SQL and Setup: (https://www.microsoft.com/en-in/sql-server/sql-server-downloads)
3. Install Postman for testing the API: https://www.postman.com/downloads/

## Create Project

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/cnyxdqdojskk1g1dpq3h.png)

- Select ASP.NET Web Api
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/kuknm84nbt35udsxoy7b.png)

- Add Project name as MovieApiWithEF
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/e6bewwopkc2go6gb8gfb.png)

- Click on Create button it will create brand new project.
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/oax0lm2yayj75gq4hcrk.PNG)



## Add Dependencies

1. Install EF Core Design: `Install-Package Microsoft.EntityFrameworkCore.Design -Version 6.0.0`
2. Install EF Core Tools: `Install-Package Microsoft.EntityFrameworkCore.Tools -Version 6.0.0`
3. Install EF Core SQL: `Install-Package Microsoft.EntityFrameworkCore.SqlServer -Version 6.0.2`


## Connect Project to Database

- _Models: A model is an object that represents the data in your application(define the database entities)._

(1) Create `Models` folder. In the same folder create `Movie.cs` file.

**Movie.cs**
```
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;

namespace MovieApiWithEF.Models
{
    public class Movie
    {
        //[DatabaseGenerated(DatabaseGeneratedOption.None)]
        [Key]
        public int Id { get; set; }

        [Required]
        public string Title { get; set; }

        [Required]
        public string Genre { get; set; }
        public DateTime? ReleaseDate { get; set; }
        public string? Director { get; set; }
        
    }
}

```


(2) Create `EfCore` folder. Create `MovieAppDbContext.cs` file in the same folder.

_DbContext: Application code interact with the database. It is this class that manages the database connection and is used to retrieve and save data in the database._

```
using Microsoft.EntityFrameworkCore;
using MovieApiWithEF.Models;

namespace MovieApiWithEF.EfCore
{
    public class MovieAppDbContext: DbContext
    {
        public DbSet<Movie> Movies { get; set; }

        public MovieAppDbContext(DbContextOptions<MovieAppDbContext> options) : base(options)
        {

        }
    }
}

   
```
(3) Open `appsetings.json` file and add following connection string with `MovieApi` database name.

_Connection Strings: Keep standard configuration information in connection string. Setup all database related information like server name, database name, user-id, password. Add a connection string property to the appSettings.json file and refer to your DbContext class inside Startup.cs file along with connection string. You will be all set to call your API to connect to a single SQL database for now._

```
"ConnectionStrings": {
    "DefaultConnection": "Server=(localdb)\\mssqllocaldb;Database=MovieWebApi;Trusted_Connection=True;MultipleActiveResultSets=True"
  },
```
(4) Open `Program.cs` file and add following code for configuring as a service.

_UseSqlServer: UseSqlServer() method configures the context to use a sql server database and takes a string representing the connection string as a parameter._

```
var connectionString = builder.Configuration.GetConnectionString("DefaultConnection");
builder.Services.AddDbContext<MovieAppDbContext>(x =>
{
    x.UseSqlServer(connectionString);
});
```

(5) Open Package Manager Console run following commands.
_The migration feature in EF core provides a way to incrementally update the database schema to keep it sync with the application's data model while preserving existing data in the database_

- `add-migration AddToDb1`: Creates a migration by adding a migration snapshot.
- `update-database`: Create or update the database schema.

## Repositories
_**Repository:** Repository Pattern is an abstraction of the Data Access Layer. Repositories are classes that hide the logics required to store or retreive data._

(1) Create `Repository` folder. In that create Interface named as IMovieRepository

```
using MovieApiWithEF.Models;

namespace MovieApiWithEF.Repository
{
    public interface IMovieRepository
    {
        IQueryable<Movie> GetMovies();

        Movie GetMovie(int id);

        bool MovieExists(int id);

        bool MovieExists(string title);

        bool CreateMovie(Movie movie);

        bool UpdateMovie(Movie movie);

        bool DeleteMovie(Movie movie);

        bool Save();
    }
}

```

(2) Create `MovieRepository.cs` file in Repository folder.
```
using Microsoft.EntityFrameworkCore;
using MovieApiWithEF.EfCore;
using MovieApiWithEF.Models;
using MovieApiWithEF.Repository;

namespace ODataMovieApiWithEF.Repository
{
    public class MovieRepository : IMovieRepository
    {
        private readonly MovieAppDbContext _db;

        public MovieRepository(MovieAppDbContext db)
        {
            _db = db;
        }
        public bool CreateMovie(Movie movie)
        {
            _db.Movies.Add(movie);
            return Save();
        }

        public bool DeleteMovie(Movie movie)
        {
            _db.Movies.Remove(movie);
            return Save();
        }

        public IQueryable<Movie> GetMovies()
        {
            return _db.Movies.AsQueryable();

        }

        public bool Save()
        {
            return _db.SaveChanges() >= 0 ? true : false;
        }

        public bool UpdateMovie(Movie movie)
        {
            _db.Movies.Update(movie);
            return Save();
        }

        public bool MovieExists(int id)
        {
            return _db.Movies.Any(x => x.Id == id);
        }

        public Movie GetMovie(int id)
        {
            return _db.Movies.FirstOrDefault(x => x.Id == id);
        }

        public bool MovieExists(string title)
        {
            bool value = _db.Movies.Any(y => y.Title.ToLower().Trim() == title.ToLower().Trim());
            return value;
        }
    }
}


```

(3)Dependency Injection (DI) 
_**Dependency Injection (DI):** Is a technique for achieving loose coupling between objects and their collaborators, or dependencies_
_There are three ways by which dependencies can be registered:_

1. AddSingleton: Singleton lifetime services are created the first time they are requested (or when ConfigureServices is run if you specify an instance there) and then every subsequent request will use the same instance.
2. AddScoped: Scoped lifetime services are created once per request.
3. AddTransient: Transient lifetime services are created each time they are requested. This lifetime works best for lightweight, stateless services.

```
builder.Services.AddScoped<IMovieRepository, MovieRepository>();
```


## Create API controller
_**Controller:** A controller is responsible for controlling the way that a user interacts with an application. A controller contains the flow control logic. It determines what response to send back to a user when a user makes a browser request_

- Create a `MoviesController.cs` file under Controllers folder.

- **Routing:** Routing is responsible for matching incoming HTTP requests and dispatching those requests to the app's executable endpoints.

- The **[ApiController]** attribute applies inference rules for the default data sources of action parameters. These rules save you from having to identify binding sources manually by applying attributes to the action parameters.

- **ControllerBase:** The ControllerBase Class in provides many methods and properties to handle HTTP Requests and Responses.

- **IQueryable:** IQueryable interface provide the functionality to query data against a specific data source.

```

using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.OData.Query;
using Microsoft.AspNetCore.OData.Routing.Controllers;
using MovieApiWithEF.Models;
using MovieApiWithEF.Repository;

namespace MovieApiWithEF.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class MoviesController : ControllerBase
    {
        private readonly IMovieRepository _movieRepo;
        public MoviesController(IMovieRepository movieRepo)
        {
            _movieRepo = movieRepo;
        }

        /// <summary>
        /// Get list of all Movies
        /// </summary>
        [HttpGet]

        public IQueryable Get()
        {
            return _movieRepo.GetMovies();
        }

        /// <summary>
        /// Create a new movie
        /// </summary>
        [HttpPost]
        public async Task<IActionResult> Post([FromBody] Movie movie)
        {
            if (movie == null)
                return BadRequest(ModelState);
            if (_movieRepo.MovieExists(movie.Title))
            {
                ModelState.AddModelError("", "Movie already Exist");
                return StatusCode(500, ModelState);
            }

            if (!_movieRepo.CreateMovie(movie))
            {
                ModelState.AddModelError("", $"Something went wrong while saving movie record of {movie.Title}");
                return StatusCode(500, ModelState);
            }

            return Ok(movie);

        }

        /// <summary>
        /// Update a movie
        /// </summary>
        /// <return></return>
        [HttpPut("{movieId:int}")]
        public IActionResult Update(int movieId, [FromBody] Movie movie)
        {
            if (movie == null || movieId != movie.Id)
                return BadRequest(ModelState);

            if (!_movieRepo.UpdateMovie(movie))
            {
                ModelState.AddModelError("", $"Something went wrong while updating movie : {movie.Title}");
                return StatusCode(500, ModelState);
            }

            return NoContent();
        }

        /// <summary>
        /// Update a movie
        /// </summary>
        /// <return></return>
       
        [HttpDelete("{movieId:int}")]
        public IActionResult Delete(int movieId)
        {
            if (!_movieRepo.MovieExists(movieId))
            {
                return NotFound();
            }

            var movieobj = _movieRepo.GetMovie(movieId);

            if (!_movieRepo.DeleteMovie(movieobj))
            {
                ModelState.AddModelError("", $"Something went wrong while deleting movie : {movieobj.Title}");
                return StatusCode(500, ModelState);
            }

            return NoContent();
        }

    }
}

```

## Testing API on Swagger

- When we run our project it will show following actions to perform. 

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/kzo2t3eclbz1fwwrd3lu.PNG)

-Testing **GET** action: Return all movie objects from database

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/uwtq78wka5bohgane8dm.PNG)

-Testing **POST** action: Create a new movie object.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/uyu4r2lkt936x55ywi6j.PNG)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9fx4og13c9gs2lddd5dg.PNG)

-Testing **PUT** action: Updating the object with `"genre: Action" `

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/tzs3r7cjr1u0co4wt5qq.PNG)

-Testing **Delete** action: Deleting the object with id=3

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/jj9bjo05cgj79tufh4d4.PNG)


## Repository

You can check for the source code here.
[MovieApi-With-Entity-Framework](https://github.com/renukapatil-21/MovieApi-With-Entity-Framework-)

Keep Learning!
**Thank You**
_Hope this helps, feel free to share your ideas or comment on this article and let me know your thoughts or if you have any questions!_