Featured Article

Build Modern APIs with ASP.NET Core Minimal APIs Without Controller

Kenneth Jul 13, 2026

In the ever-evolving landscape of web development, ASP.NET Core has emerged as a robust framework for building modern, cloud-based, highly scalable web applications. While ASP.NET Core Web API typically relies on controllers to handle requests and responses, it's possible to create APIs without controllers, leveraging the power of middleware and action invokers. Let's delve into this alternative approach, exploring its advantages, implementation, and best practices.

an image of the api logo surrounded by different types of data and icons on a white background
an image of the api logo surrounded by different types of data and icons on a white background

Creating an ASP.NET Core Web API without controllers can offer enhanced testability, separation of concerns, and the flexibility to handle complex routing scenarios. It can also lead to cleaner, more maintainable code, as the routing logic and action handling are decoupled from the HTTP methods.

ASP.NET Web API Tutorials For Beginners and Professionals
ASP.NET Web API Tutorials For Beginners and Professionals

Setting Up an ASP.NET Core Web API Without Controllers

To create an ASP.NET Core Web API without controllers, you'll need to understand the MVC pipeline, middleware, and action invokers. The first step involves setting up your project and the necessary dependencies.

Asp.net Core web API Tutorial: C# web API .Net Core Example
Asp.net Core web API Tutorial: C# web API .Net Core Example

1. **Create a new ASP.NET Core Web API project**: You can use the .NET CLI to create a new project with the 'webapi' template: `dotnet new webapi -n MyApi`.

Configure the Startup.cs File

a web api with asp net core, net 6 0 build a web api with asp net core
a web api with asp net core, net 6 0 build a web api with asp net core

The Startup.cs file is where you'll configure the middleware and routing components. Here's a basic example of how to set up the pipeline without controllers:

```csharp public void Configure(IApplicationBuilder app, IWebHostEnvironment env) { app.UseRouting(); app.UseEndpoints(endpoints => { endpoints.MapControllers(); // Remove this line if using only middleware endpoints.MapGet("/api/example", async context => { await context.Response.WriteAsync("This is an example endpoint handling a GET request."); }); }); app.Run(async context => { await context.Response.WriteAsync("Fallback route matched."); }); } ```

Handling Different HTTP Methods

asp net core web api without controller
asp net core web api without controller

To handle different HTTP methods like POST, PUT, or DELETE, you can use the corresponding `Map` methods in the `UseEndpoints` extension:

```csharp endpoints.MapPost("/api/example", async context => { // Handle POST request... }); ```

Implementing Business Logic Without Controllers

ASP.Net Projects with Source Code
ASP.Net Projects with Source Code

Without controllers, you'll need to decide how to handle your business logic. Here are a couple of common approaches:

Using Services

Implementing Web APIs: Connect & Fetch Data Like a Pro 🌍🚀
Implementing Web APIs: Connect & Fetch Data Like a Pro 🌍🚀
Web Application, 10 Things
Web Application, 10 Things
#ASP.Net Core benefits
#ASP.Net Core benefits
Fully basic CRUD Operation using ASP.NET Core Web API Example [For Beginners]
Fully basic CRUD Operation using ASP.NET Core Web API Example [For Beginners]
🌟12 Essential Tips to Secure Your API
🌟12 Essential Tips to Secure Your API
the rest api method is shown in this screenshote image, which shows how to use
the rest api method is shown in this screenshote image, which shows how to use
the asp net page lifecycle
the asp net page lifecycle
REST API Methods Explained in 60 Seconds
REST API Methods Explained in 60 Seconds
what is api? - screenshote for application programming and web development in the usa
what is api? - screenshote for application programming and web development in the usa

Developing services for each domain functionality allows for better separation of concerns and testability. You can inject these services into your middleware or action invokers.

```csharp public class ExampleService { public string GetExample() => "Example data..."; } ```

Using Async Delegates

You can also define async delegates to handle the request and response logic directly in your middleware or action invokers, keeping it decoupled from any specific service or model.

Testing ASP.NET Core Web API Without Controllers

Testing APIs created without controllers can be more straightforward, as you're not dealing with controller-specific code. You can use tools like Moq and xUnit to create unit tests for your services and middleware.

In conclusion, while ASP.NET Core Web API usually relies on controllers, exploring alternatives can lead to more maintainable, testable, and flexible code. Yet, it requires careful planning and a solid understanding of the MVC pipeline. By leveraging middleware, action invokers, and services, you can create robust APIs tailored to your needs. Keep experimenting and weighing the pros and cons to choose the best approach for your projects.