Building Your First RESTful API with ASP.NET Core WEB API (C#)

Hello everyone, welcome to this article, I am pleased to serve you with an amazing content related to C#. I have been working on .NET C# APIs for last 6 years, so I believe, I have enough expertise to express my knowledge with you. So here is my content outline:

Content Outline in this article:

  1. What is Restful API?
  2. What is ASP.NET Core WEB API?
  3. Sample Project
  4. Git and YouTube Link
  5. Conclusion and Call to Action

So Lets get started with some basic topics of this article:

What is Restful API?

  • RESTful APIs allow applications to communicate over HTTP.
  • Based on principles like statelessness and resource representation.
  • Common HTTP Methods: GET, POST, PUT, DELETE.

What is ASP.NET Core WEB API?

It is a framework for communication between applications over internet and it has a build-in support for REST APIs.

Sample Project

Step :1 You have to select first template from Visual Studio (2019 or 2022) after searching web Api

Step 2: Keep this setting in 3rd screen, in 2nd screen you just have to specify the path of your project

This will be created in solution explorer

You can see in the right side: there are some important files like:

  • Program.cs : Entry point of application, it configures and starts the main application
  • Controller Folder : For controllers and apis to be used
  • Appsettings.json : This will be used for configuration data; it will contain database connection string or other folders configuration data like logging or other files

So, let’s get started with the code, use case for this tutorial will be CRUD operations over Car model, like add car, delete car, update car, and read car.

Step: 4 Create a Car Model

namespace CRUDWEBAPICORE8
{
    public class Car
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public string Description { get; set; }        
    }
}

Step 5: Create a Controller named as CarContoller.cs

using Microsoft.AspNetCore.Mvc;

namespace CRUDWEBAPICORE8.Controllers
{
    [ApiController]
    [Route("api/[controller]")]
    public class CarController : ControllerBase
    {

        private static List<Car> cars = new List<Car>
        { 
            new Car() { Id = 1 , Name = "Civic" , Description = "CivicModel" },        
            new Car() { Id = 2 , Name = "Honda" , Description = "Honda" },
            new Car() { Id = 3 , Name = "Suzuki" , Description = "Suzuki" }
        };


        [HttpGet]
        public IActionResult GetAll()
        {
            return Ok(cars);
        }

        [HttpGet("{id}")]
        public IActionResult Get(int id)
        {
            var foundCar = cars.FirstOrDefault(x => x.Id == id);

            if (foundCar == null)
            {
                return NotFound();
            }

            return Ok(foundCar);
        }

        [HttpPost]
        public IActionResult Create(Car car)
        {
            cars.Add(car);
            return Ok(cars);
        }

        [HttpDelete]
        public IActionResult Delete(int id)
        {
            var foundCar = cars.FirstOrDefault(x => x.Id == id);

            if (foundCar == null)
            {
                return NotFound();
            }

            cars.Remove(foundCar);

            return Ok(cars);
        }

        [HttpPut("{id}")]
        public IActionResult Update(int id, Car car)
        {
            var foundCar = cars.FirstOrDefault(x => x.Id == id);

            if (foundCar == null)
            {
                return NotFound();
            }

            foundCar.Name = car.Name;
            foundCar.Description = car.Description;

            return NoContent();
        }

    }
}

Step 6: This is unchanged Program.cs

var builder = WebApplication.CreateBuilder(args);

builder.Services.AddControllers();
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();

var app = builder.Build();

if (app.Environment.IsDevelopment())
{
    app.UseSwagger();
    app.UseSwaggerUI();
}

app.UseHttpsRedirection();
app.UseAuthorization();
app.MapControllers();
app.Run();

Step: 7 Run Application and Test swagger

Git and YouTube Link

https://github.com/saifFast/CRUDWEBAPICORE8

Conclusion

Building a RESTful API with ASP.NET Core Web API in C# is a powerful way to create scalable and efficient backend services. By understanding the basics of project setup, routing, controllers, and CRUD operations, you’ve taken a significant step toward mastering API development.

Next Steps

If you found this If you found this tutorial helpful, don’t stop here! Explore more advanced topics in ASP.NET Core Web API development and keep building your skills. article helpful, don’t forget to share it with your peers who could benefit too!

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top