Introduction
In my last article, we discussed the basics of ASP.NET Core APIs and how to create an API using Entity Framework. Finally, we ended up with creating a Get API which is used to fetch the details of all the authors from the back-end as a list. Now, in this article, we are going to see how to create a CRUD (Create, Read, Update, Delete) API in ASP.NET Core.
This is a continuation of my last article (create RESTful API using ASP.NET Core with Entity Framework Core), so I highly recommend you to go through my previous article for the basic set up of an ASP.NET Core application.
CREATE
I’m using my existing application that we created in my last article for a demo. Open the LibraryRepository.cs file from the application and write the below code.
- public Author PostAuthor(Author author)
- {
- try
- {
- if(_libraryContext!=null)
- {
- _libraryContext.Add(author);
- _libraryContext.SaveChanges();
- return author;
- }
- else
- {
- return null;
- }
- }
- catch(Exception ex)
- {
- //log exception
- return null;
- }
- }
_libraryContext is our database context, the Add method is used to add the records in the entity, the SaveChanges method is used to save the changes in the database.
Open LibrariesController.cs file from the application and write this code.
- [HttpPost]
- [Route("AddAuthor")]
- public IActionResult AddAuthor([FromBody]Author authorparam)
- {
- try
- {
- if (ModelState.IsValid)
- {
- Author author = _libraryRepository.PostAuthor(authorparam);
- return Ok(author);
- }
- else
- {
- return BadRequest();
- }
- }
- catch
- {
- //log Exception
- return BadRequest();
- }
- }
The AddAuthor action will fetch the data from the body of request. Once the modelstate is validated, the data will be added to the database by calling the PostAuthor method in repository.
Let’s test the API in POSTMAN.
https://localhost:44348/api/Libraries/AddAuthor
From the above image, you can notice that the data is successfully added and returned.
Changes in the database.
READ
We already discussed about GetAllAuthor() read method in my last article. Now, I’m giving a demo of the method which is used to read the data from the database based on Id.
Open the LibraryReporsitory.cs file from the application and write the following code into it.
- public Author GetAuthor(Guid authorId)
- {
- try
- {
- return _libraryContext.Authors.Where(e => e.AuthorId == authorId).FirstOrDefault();
- }
- catch(Exception ex)
- {
- //log exception
- return null;
- }
- }
Open the LibrariesController.cs file from the application to add the below code.
- [HttpGet]
- [Route("GetAuthor")]
- public IActionResult GetAuthor(Guid authorId)
- {
- try
- {
- Author author = _libraryRepository.GetAuthor(authorId);
- return Ok(author);
- }
- catch(Exception ex)
- {
- //log exception
- return BadRequest();
- }
- }
Let’s test the API in POSTMAN.
https://localhost:44348/api/Libraries/DeleteAuthor?authorId=04dcc177-9a0a-4444-4aff-08d694b67230

UPDATE
Open LibraryRepository.cs file and update it with the given code.
- public Author UpdateAuthor(Author author)
- {
- try
- {
- if (_libraryContext != null)
- {
- _libraryContext.Update(author);
- _libraryContext.SaveChanges();
- return author;
- }
- else
- {
- return null;
- }
- }
- catch (Exception ex)
- {
- //log exception
- return null;
- }
- }



Join the conversation! Your thoughts help the community grow.