Get Started with RESTful API Development in .NET Core 7: Beginner’s Guide

Ashish Sharma
6 min readMay 11, 2023

Learn the Fundamentals of Creating Web APIs for Your Applications Using the .NET Core 7

Photo by Christina Morillo

Overview

This blog is a beginner-friendly tutorial that provides an introduction to developing RESTful APIs using .NET Core 7. The tutorial is designed for individuals who are new to API development and want to learn how to create APIs using the .NET Core framework. before starting, I want to know you, I’m trying to understand this in simple way.

The tutorial covers the basic concepts of RESTful APIs and HTTP verbs, followed by an introduction to .NET Core 7 and its features. It also explains how to set up a development environment and how to create a new .NET Core project. The tutorial then walks through the process of creating a basic RESTful API using .NET Core and demonstrates how to perform CRUD (Create, Read, Update, and Delete) operations on a simple data model.

By the end of the tutorial, the reader should have a good understanding of how to develop RESTful APIs using .NET Core 7, as well as the tools and techniques required to create high-quality and secure APIs.

Project setup

Install the .NET Core 7 SDK.

To get started with .NET Core 7 development, you need to install the .NET Core 7 SDK on your development machine. You can download the latest SDK from the official .NET Core website.
LINK: https://dotnet.microsoft.com/en-us/download/dotnet/7.0

Choose an Integrated Development Environment (IDE)

There are several IDEs available for .NET Core development, including Visual Studio, Visual Studio Code, and JetBrains Rider. Choose an IDE that you are most comfortable with and install it on your development machine. I prefer you to go with Visual studio.
LINK: https://visualstudio.microsoft.com/downloads/

Note: If you want to use free version of visual studio so download Community version

Create a new .NET Core 7 project

Once you download and install all above things then you simple open visual studio 2022 and follow below steps to create a .net core api project

  • Click on: Create a new Project
  • Search: ASP .NET Core Web API
  • Select first one which contain C# logo and click next
  • Enter project name, project location and Solution name and then click next
  • Select .NET 7 Framework and then set as default options and then finally click on create button

After complete all steps you will see below screen

here, you see their is serval folders and files

  • Connected Services: Connected services is refer to pre-configured and integrated external services that can be easily consumed in your application.
  • Dependencies: Dependencies is refer to external libraries or packages that the project relies on for its functionality.
  • Properties: The Properties folder in a .NET Core 7 API project contains assembly information and launch settings for the application.
  • Controller: The “Controllers” folder in a .NET Core 7 API project contains the classes that handle incoming HTTP requests and produce corresponding responses.
  • App Setting: The appsettings.json file is a configuration file in .NET Core 7 API projects that stores application settings such as database connection strings and logging configurations.
  • Program file: The program.cs file in a .NET Core 7 API project is the entry point of the application, which sets up and configures the web host environment.

Before going to implement API’s, first you have run the project by clicking “https” button.

The above screenshot is refer that our project is successfully created without any error.

Now, we first you have to delete 2 file’s from your project. First one is WeatherForecastController.cs and another one is WeatherForecast.cs.

Then, create a new controller named “TodoController”. This controller will have endpoints for creating, reading, updating, and deleting todos. To create TodoController file, so right click on Controller folder and navigate controller in add option.

You have see a window, where 3 options appear. you simple go with MVC Controller -Empty

Now, Give your controller name “TodoController” and click Add Button.

you have to add one file which simply refer a model file which name is Todo.cs.

It’s a simple C# file, which contain simple properties. So Copy code from below and paste it in Todo.cs file

namespace FirstProject.Model
{
public class Todo
{
public int Id { get; set; }
public string? Title { get; set; }
public string? Description { get; set; }
public bool Completed { get; set; }
}
}

And also paste below code in TodoController file.

using FirstProject.Model;
using Microsoft.AspNetCore.Mvc;

[ApiController]
[Route("[controller]")]
public class TodoController : ControllerBase
{
private readonly List<Todo> _todos;

public TodoController()
{
_todos = new List<Todo>();
}

[HttpGet]
public IEnumerable<Todo> Get()
{
return _todos;
}

[HttpGet("{id}")]
public ActionResult<Todo> GetById(int id)
{
var todo = _todos.FirstOrDefault(t => t.Id == id);
if (todo == null)
{
return NotFound();
}

return todo;
}

[HttpPost]
public ActionResult<Todo> Create(Todo todo)
{
todo.Id = _todos.Count + 1;
_todos.Add(todo);

return CreatedAtAction(nameof(GetById), new { id = todo.Id }, todo);
}

[HttpPut("{id}")]
public IActionResult Update(int id, Todo todo)
{
var existingTodo = _todos.FirstOrDefault(t => t.Id == id);
if (existingTodo == null)
{
return NotFound();
}

existingTodo.Title = todo.Title;
existingTodo.Description = todo.Description;
existingTodo.Completed = todo.Completed;

return NoContent();
}

[HttpDelete("{id}")]
public IActionResult Delete(int id)
{
var todo = _todos.FirstOrDefault(t => t.Id == id);
if (todo == null)
{
return NotFound();
}

_todos.Remove(todo);

return NoContent();
}
}

In the above code, the controller has endpoints for retrieving all todos, retrieving a specific todo by ID, creating a new todo, updating an existing todo, and deleting a todo.

The controller uses a List<Todo> to store todos in memory. In a real application, you would likely use a database or other persistent storage instead.

The [ApiController] attribute is used to enable automatic model validation and binding of parameters from the request body.

The [Route("[controller]")] attribute specifies that the controller should be accessible at the root URL followed by "/Todo" (e.g. "https://localhost:5001/Todo").

Now final run the project. and perform your first CURD Operation

I hope you’re enjoying reading my blog on Medium. If you find it helpful and informative, please take a moment to hit the “like” button at the bottom of the page. Also, feel free to share the link with your friends who might be interested in the same topic.

Your support means a lot to me and it motivates me to keep sharing more useful content. Thanks for taking the time to read my blog and for spreading the word!

--

--

Ashish Sharma

Ashish Sharma, a skilled software developer with expertise in .NET Core and MVC. Passionate about coding, problem-solving, and building innovative solutions.