Feb 03 2025
public class BlogPost { public int Id { get; set; } public string Title { get; set; } = string.Empty; public string Content { get; set; } = string.Empty; }
public class BlogDbContext : DbContext { public BlogDbContext(DbContextOptions<BlogDbContext> options) : base(options) { } public DbSet<BlogPost> BlogPosts => Set<BlogPost>(); }
app.MapGet("/api/blog", async (BlogService blogService) => await blogService.GetAllPostsAsync()); app.MapGet("/api/blog/{id:int}", async (int id, BlogService blogService) => { var post = await blogService.GetPostByIdAsync(id); return post is null ? Results.NotFound() : Results.Ok(post); }); app.MapPost("/api/blog", async (BlogPost post, BlogService blogService) => { var createdPost = await blogService.AddPostAsync(post); return Results.Created($"/api/blog/{createdPost.Id}", createdPost); }); app.MapDelete("/api/blog/{id:int}", async (int id, BlogService blogService) => { return await blogService.DeletePostAsync(id) ? Results.NoContent() : Results.NotFound(); });
**


"ConnectionStrings": { "Development": "COPY_YOUR_DEVELOPMENT_CONNECTION_STRING", "Test": "COPY_YOUR_TEST_CONNECTION_STRING", "Production": "COPY_YOUR_PRODUCTION_CONNECTION_STRING" }
var builder = DistributedApplication.CreateBuilder(args); var environment = builder.Environment.EnvironmentName; var connectionString = builder.AddConnectionString(environment); builder.AddProject<Projects.Blog_Api>("blog-api") .WithReference(connectionString); builder.Build().Run();
var env = builder.Environment.EnvironmentName; var connectionString = builder.Configuration.GetConnectionString(env); builder.Services.AddDbContext<BlogDbContext>(options => options.UseNpgsql(connectionString)); builder.Services.AddScoped<BlogService>();



1. Design Patterns that Deliver
This isn’t just another design patterns book. Dive into real-world examples and practical solutions to real problems in real applications.Check out it here.
Go-to resource for understanding the core concepts of design patterns without the overwhelming complexity. In this concise and affordable ebook, I've distilled the essence of design patterns into an easy-to-digest format. It is a Beginner level. Check out it here.
Every Monday morning, I share 1 actionable tip on C#, .NET & Arcitecture topic, that you can use right away.
Join 18,000+ subscribers to improve your .NET Knowledge.
Subscribe to the TheCodeMan.net and be among the 18,000+ subscribers gaining practical tips and resources to enhance your .NET expertise.