Creating a Simple ASP.NET Core Web API for Testing Purposes
Tags: webdev, api, aspdotnet, tutorial
Published: 2024-07-05 16:16:18

## Introduction
ASP.NET Core NET Core is a powerful framework for building web APIs. In this tutorial, we will walk you through creating a simple API for testing. Whether you are a beginner or looking to refresh your knowledge, this guide will help you set up a basic web API project in no time.

## Prerequisites
Before we begin, ensure you have the following installed on your development machine:

- Visual Studio (2019 or later)
- .NET Core SDK (I am using 8 for this tutorial)

## Step-by-Step Guide to Creating a Simple ASP.NET Core API
**Step 1: Create a New Web API Project**

- Open Visual Studio.
- Click on Create a new project.
- Select the template ASP.NET Core NET Core Web API Application and click Next.

![Create a new project](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/hyqoigjrj4ow04esr37v.png)

- Name your project and click Next.
- Provide Additional information. I am selecting . NET 8.0 as a framework. Click Create.

![additional info](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/wfufkq8p8z92h1j60ywc.png)

- Once the project gets created. You will find one controller by default available called WeatherForecastController.

![api](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/38jyp42zbxn2dbft5fpo.png)

**Step 2: Create our API**

- Right click on Controllers > Add > Controller.
- Click on API on the left and select API Controller — Empty.

![Create controller](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/syjy5t9dvgaayizcc69l.png)

- Give it a name and click on Add.
- Add the following code. I am adding one get method which is just returning “Hello World!”.

```
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
namespace TestApi.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class TestApiController : ControllerBase
    {
        [HttpGet]
        public IActionResult Get()
        {
            return Ok(new { Message = "Hello, World!" });
        }
    }
}
```
**Step 3: Run the Application**

- Press F5 to run the application.
- You will see the Swagger UI with your API endpoints listed.

![run project](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ziphv05qq7vxq5mvsvv0.png)
**Step 4: Test the API Endpoints**

- Using the Swagger UI, you can test the endpoints.
- Click on execute and it will give the data as response.

![test api](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xi6k8sasz32ke7yfhllf.png)

- If you want to check which URL it’s hitting then open developer tools and go to network tab. Click on execute button for the endpoint and you will see one request on the network. Click on it. On the Headers tab, you will get the Request URL. That’s the API URL.

![Inspect](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/tmk0h84jgr0j5avrua47.png)

- You can even run the URL on the new tab and it will give the response.

![Output](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/e651gxo7yfenmsq1lz3c.png)

## Conclusion
Congratulations! You’ve successfully created a simple ASP.NET Core NET Core API for testing purposes. This basic setup can serve as a foundation for more complex APIs. Remember, practice makes perfect, so continue experimenting and building upon what you’ve learned.

In the next blog, we will schedule this test API call with Hangfire. Here’s the [link](https://shekhartarare.com/Archive/2024/6/step-by-step-guide-to-scheduling-api-calls-with-hangfire) for that blog.