If you’re looking to build a lightweight, fast, and production-ready REST API using ASP.NET Core and SQLite, you’re in the right place. In this tutorial, we’ll walk through creating a basic API for a blog with endpoints to manage posts.
We’ll use:
- ASP.NET Core Web API
- SQLite as a lightweight database
- Entity Framework Core for ORM
Prerequisites
Before we begin, make sure you have:
- .NET 8 SDK
- A code editor like Visual Studio Code or Visual Studio
- Basic knowledge of C# and REST APIs
Step 1: Create the Project
Open your terminal and run:
dotnet new webapi -n BlogApi
cd BlogApi
Step 2: Install SQLite & EF Core Packages
dotnet add package Microsoft.EntityFrameworkCore.Sqlite
dotnet add package Microsoft.EntityFrameworkCore.Tools
Step 3: Create the BlogPost Model
Create a Models
folder and add BlogPost.cs
:
namespace BlogApi.Models
{
public class BlogPost
{
public int Id { get; set; }
public string Title { get; set; } = string.Empty;
public string Content { get; set; } = string.Empty;
public DateTime CreatedAt { get; set; } = DateTime.UtcNow;
}
}
Step 4: Setup the DB Context
Create a Data
folder and add BlogContext.cs
:
using Microsoft.EntityFrameworkCore;
using BlogApi.Models;
namespace BlogApi.Data
{
public class BlogContext : DbContext
{
public BlogContext(DbContextOptions<BlogContext> options) : base(options) { }
public DbSet<BlogPost> BlogPosts { get; set; }
}
}
Step 5: Register Services in Program.cs
Update Program.cs
to include SQLite and our DB context:
using BlogApi.Data;
using Microsoft.EntityFrameworkCore;
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddControllers();
builder.Services.AddDbContext<BlogContext>(options =>
options.UseSqlite("Data Source=blog.db"));
var app = builder.Build();
app.UseHttpsRedirection();
app.UseAuthorization();
app.MapControllers();
app.Run();
Step 6: Add the BlogPosts Controller
Create a Controllers
folder and add BlogPostsController.cs
:
using Microsoft.AspNetCore.Mvc;
using BlogApi.Data;
using BlogApi.Models;
using Microsoft.EntityFrameworkCore;
namespace BlogApi.Controllers
{
[ApiController]
[Route("api/[controller]")]
public class BlogPostsController : ControllerBase
{
private readonly BlogContext _context;
public BlogPostsController(BlogContext context)
{
_context = context;
}
[HttpGet]
public async Task<ActionResult<IEnumerable<BlogPost>>> GetAll()
{
return await _context.BlogPosts.ToListAsync();
}
[HttpGet("{id}")]
public async Task<ActionResult<BlogPost>> Get(int id)
{
var post = await _context.BlogPosts.FindAsync(id);
return post == null ? NotFound() : post;
}
[HttpPost]
public async Task<ActionResult<BlogPost>> Create(BlogPost post)
{
_context.BlogPosts.Add(post);
await _context.SaveChangesAsync();
return CreatedAtAction(nameof(Get), new { id = post.Id }, post);
}
[HttpPut("{id}")]
public async Task<IActionResult> Update(int id, BlogPost post)
{
if (id != post.Id) return BadRequest();
_context.Entry(post).State = EntityState.Modified;
await _context.SaveChangesAsync();
return NoContent();
}
[HttpDelete("{id}")]
public async Task<IActionResult> Delete(int id)
{
var post = await _context.BlogPosts.FindAsync(id);
if (post == null) return NotFound();
_context.BlogPosts.Remove(post);
await _context.SaveChangesAsync();
return NoContent();
}
}
}
Step 7: Create & Migrate the Database
Run the following in the terminal:
dotnet ef migrations add InitialCreate
dotnet ef database update
SQLite will create a blog.db
file in your project directory.
Step 8: Run the API
dotnet run
Open your browser or Postman and test endpoints like:
GET /api/blogposts
POST /api/blogposts
PUT /api/blogposts/{id}
DELETE /api/blogposts/{id}
Conclusion
You’ve just built a fully functional REST API using ASP.NET Core and SQLite. This setup is perfect for prototyping, small projects, or even lightweight production APIs.
Next steps:
- Add validation using
DataAnnotations
- Secure endpoints with JWT
- Paginate large result sets
Let me know if you want a follow-up post on any of those!