Core API Creating the First Project
With the environment ready, it is time to create the BookStore API project. This topic walks through every step of creating a new ASP.NET Core Web API project and explains what gets generated automatically.
Creating the Project in Visual Studio 2022
Follow these steps inside Visual Studio 2022:
- Open Visual Studio 2022
- Click Create a new project
- Search for ASP.NET Core Web API and select it
- Click Next
- Set the Project name to
BookStoreAPI - Choose a folder location
- Click Next
- Set Framework to .NET 8.0
- Make sure Use controllers is checked
- Make sure Enable OpenAPI support is checked (adds Swagger)
- Click Create
Creating the Project Using the CLI (Alternative)
If using Visual Studio Code or a terminal, run these commands:
mkdir BookStoreAPI
cd BookStoreAPI
dotnet new webapi --name BookStoreAPI
This creates the same project structure that Visual Studio 2022 generates.
Understanding the Generated Project Structure
After creating the project, Visual Studio generates these files and folders:
BookStoreAPI/
├── Controllers/
│ └── WeatherForecastController.cs ← Sample controller (can be deleted)
├── Properties/
│ └── launchSettings.json ← Dev server configuration
├── appsettings.json ← App configuration (DB connection, etc.)
├── appsettings.Development.json ← Dev-only settings
├── Program.cs ← Application entry point
├── WeatherForecast.cs ← Sample model (can be deleted)
└── BookStoreAPI.csproj ← Project file (packages, SDK version)
The sample WeatherForecastController.cs and WeatherForecast.cs are just examples. Delete them and replace with the BookStore-specific files as the course progresses.
Understanding Program.cs
Program.cs is the starting point of the entire application. Every service, middleware, and configuration is registered here. In .NET 6 and later, it uses a minimal hosting model — no separate Startup.cs file.
var builder = WebApplication.CreateBuilder(args);
// Step 1: Register services
builder.Services.AddControllers();
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();
var app = builder.Build();
// Step 2: Configure the middleware pipeline
if (app.Environment.IsDevelopment())
{
app.UseSwagger();
app.UseSwaggerUI();
}
app.UseHttpsRedirection();
app.UseAuthorization();
app.MapControllers();
app.Run();
There are two sections in Program.cs:
| Section | What It Does |
|---|---|
builder.Services.Add...() | Register services (like controllers, Swagger, database) |
app.Use...() | Add middleware that processes every HTTP request |
Understanding appsettings.json
appsettings.json stores configuration values like database connection strings, JWT secrets, and logging levels. This file is read at startup.
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
},
"AllowedHosts": "*"
}
Later in this course, a database connection string for the BookStore API will be added here:
{
"ConnectionStrings": {
"BookStoreDB": "Server=(localdb)\\mssqllocaldb;Database=BookStoreDB;Trusted_Connection=True;"
}
}
Creating the Book Model
Delete the generated sample files and create the first BookStore-specific file. Add a Models folder and create Book.cs inside it:
// Models/Book.cs
namespace BookStoreAPI.Models
{
public class Book
{
public int Id { get; set; }
public string Title { get; set; } = string.Empty;
public string Author { get; set; } = string.Empty;
public decimal Price { get; set; }
public string Category { get; set; } = string.Empty;
public bool IsAvailable { get; set; }
public DateTime CreatedDate { get; set; }
}
}
Creating the First Controller
Add a BooksController.cs file inside the Controllers folder. For now, it returns a hardcoded list of books. The database connection comes in a later topic.
// Controllers/BooksController.cs
using Microsoft.AspNetCore.Mvc;
using BookStoreAPI.Models;
namespace BookStoreAPI.Controllers
{
[ApiController]
[Route("api/[controller]")]
public class BooksController : ControllerBase
{
[HttpGet]
public IActionResult GetAll()
{
var books = new List<Book>
{
new Book { Id = 1, Title = "Clean Code", Author = "Robert C. Martin",
Price = 29.99m, Category = "Technology", IsAvailable = true,
CreatedDate = DateTime.Now },
new Book { Id = 2, Title = "The Pragmatic Programmer", Author = "David Thomas",
Price = 34.99m, Category = "Technology", IsAvailable = true,
CreatedDate = DateTime.Now }
};
return Ok(books);
}
}
}
Running the Project
Press F5 in Visual Studio (or run dotnet run in the terminal). The browser opens automatically to the Swagger UI page.
https://localhost:7001/swagger
In Swagger, click on GET /api/books, then click Try it out, then Execute. The response shows:
HTTP 200 OK
[
{
"id": 1,
"title": "Clean Code",
"author": "Robert C. Martin",
"price": 29.99,
"category": "Technology",
"isAvailable": true,
"createdDate": "2024-01-15T10:00:00"
},
{
"id": 2,
"title": "The Pragmatic Programmer",
"author": "David Thomas",
"price": 34.99,
"category": "Technology",
"isAvailable": true,
"createdDate": "2024-01-15T10:00:00"
}
]
The BookStore API is running and returning data. The project structure is now set. Every topic from here builds on top of this same project.
Testing with Postman
Open Postman and create a new request:
| Setting | Value |
|---|---|
| Method | GET |
| URL | https://localhost:7001/api/books |
Click Send. The same JSON list of books appears in the Postman response panel.
Key Points
- A new Web API project is created using the ASP.NET Core Web API template in Visual Studio 2022.
Program.csis the entry point — services are registered first, then middleware is configured.appsettings.jsonstores configuration values like connection strings.- The
Bookmodel represents the data structure used throughout this course. - The
BooksControlleris the first controller that handles API requests for books. - Swagger UI provides a built-in test interface for all API endpoints.
