Scaffold ASP.NET Core Web API with controllers and middleware
✓Works with OpenClaudeYou are an ASP.NET Core developer. The user wants to scaffold a new ASP.NET Core Web API project with controllers, middleware, and proper dependency injection setup.
What to check first
- Run
dotnet --versionto verify .NET SDK is installed (6.0 or higher recommended) - Confirm you have a terminal open in the directory where you want to create the project
Steps
- Create a new ASP.NET Core Web API project using
dotnet new webapi -n MyApiProject - Navigate into the project directory with
cd MyApiProject - Review the generated
Program.csfile — this is where middleware and services are configured in .NET 6+ - Create a new controller by adding a file
Controllers/ItemsController.csthat inherits fromControllerBase - Add the
[ApiController]and[Route("api/[controller]")]attributes to your controller class - Implement HTTP action methods using
[HttpGet],[HttpPost],[HttpPut],[HttpDelete]attributes - Register any custom services in
Program.csusingbuilder.Services.AddScoped<IMyService, MyService>() - Add middleware in
Program.csbeforeapp.Run()usingapp.UseMiddleware<CustomMiddleware>()or built-ins likeapp.UseAuthentication() - Run the API with
dotnet runand test endpoints using a tool like Postman orcurl
Code
// Program.cs - Main application setup with middleware and DI
var builder = WebApplicationBuilder.CreateBuilder(args);
// Add services to the container
builder.Services.AddControllers();
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();
// Register custom services
builder.Services.AddScoped<IItemService, ItemService>();
builder.Services.AddSingleton<ILogger, ConsoleLogger>();
var app = builder.Build();
// Configure the HTTP request pipeline
if (app.Environment.IsDevelopment())
{
app.UseSwagger();
app.UseSwaggerUI();
}
app.UseHttpsRedirection();
app.UseAuthorization();
app.MapControllers();
app.Run();
// ============================================
// Controllers/ItemsController.cs
[ApiController]
[Route("api/[controller]")]
public class ItemsController : ControllerBase
{
private readonly IItemService _itemService;
public ItemsController(IItemService itemService)
{
_itemService = itemService;
}
[HttpGet("{id}")]
public async Task<ActionResult<ItemDto>> GetItem(int id)
{
var item = await _itemService.GetItemAsync(id);
if (item == null)
return NotFound();
return Ok(item);
}
Note: this example was truncated in the source. See the GitHub repo for the latest full version.
Common Pitfalls
- Treating this skill as a one-shot solution — most workflows need iteration and verification
- Skipping the verification steps — you don't know it worked until you measure
- Applying this skill without understanding the underlying problem — read the related docs first
When NOT to Use This Skill
- When a simpler manual approach would take less than 10 minutes
- On critical production systems without testing in staging first
- When you don't have permission or authorization to make these changes
How to Verify It Worked
- Run the verification steps documented above
- Compare the output against your expected baseline
- Check logs for any warnings or errors — silent failures are the worst kind
Production Considerations
- Test in staging before deploying to production
- Have a rollback plan — every change should be reversible
- Monitor the affected systems for at least 24 hours after the change
Related .NET / C# Skills
Other Claude Code skills in the same category — free to download.
Entity Framework Core
Set up Entity Framework Core with migrations and queries
ASP.NET Auth
Configure ASP.NET Identity with JWT and cookie authentication
Minimal API
Build minimal APIs with endpoint mapping
Blazor
Create Blazor components for interactive web UI
.NET Testing
Write xUnit tests with Moq and FluentAssertions
SignalR
Implement real-time communication with SignalR
.NET Background Services
Create background services with IHostedService
Want a .NET / C# skill personalized to YOUR project?
This is a generic skill that works for everyone. Our AI can generate one tailored to your exact tech stack, naming conventions, folder structure, and coding patterns — with 3x more detail.