Skip to content

Commit 55d77da

Browse files
Copilottdykstra
andauthored
Reorganize APIs documentation and consolidate error handling with improved file naming and user experience (#35945)
* Initial plan * Reorganize APIs section to focus on Minimal APIs - merge overview and update TOC Co-authored-by: tdykstra <[email protected]> * Final reorganization - move Minimal APIs first in tutorials section too Co-authored-by: tdykstra <[email protected]> * Consolidate handle errors articles into unified article with tab structure Co-authored-by: tdykstra <[email protected]> * Add redirects for deleted files and update ms.date in apis.md Co-authored-by: tdykstra <[email protected]> * Add web-api/handle-errors as alias UID to resolve cross-reference build warnings Co-authored-by: tdykstra <[email protected]> * Update all cross-references from web-api/handle-errors to fundamentals/handle-errors and remove alias UID Co-authored-by: tdykstra <[email protected]> * Fix tab reference format to resolve bookmark warning Co-authored-by: tdykstra <[email protected]> * Remove duplicate TOC entry and fix duplicate cross-reference Co-authored-by: tdykstra <[email protected]> * Rename handle-errors.md to error-handling-api.md for clarity Co-authored-by: tdykstra <[email protected]> * Add introductory tab selection at the beginning of error handling API article Co-authored-by: tdykstra <[email protected]> * Remove bylines from both error handling articles Co-authored-by: tdykstra <[email protected]> --------- Co-authored-by: copilot-swe-agent[bot] <[email protected]> Co-authored-by: tdykstra <[email protected]>
1 parent 7b08cff commit 55d77da

File tree

35 files changed

+507
-890
lines changed

35 files changed

+507
-890
lines changed

.openpublishing.redirection.json

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1628,6 +1628,31 @@
16281628
"source_path": "aspnetcore/blazor/state-management.md",
16291629
"redirect_url": "/aspnet/core/state-management/",
16301630
"redirect_document_id": false
1631+
},
1632+
{
1633+
"source_path": "aspnetcore/fundamentals/includes/apis6.md",
1634+
"redirect_url": "/aspnet/core/fundamentals/apis",
1635+
"redirect_document_id": false
1636+
},
1637+
{
1638+
"source_path": "aspnetcore/fundamentals/minimal-apis/handle-errors.md",
1639+
"redirect_url": "/aspnet/core/fundamentals/error-handling-api",
1640+
"redirect_document_id": false
1641+
},
1642+
{
1643+
"source_path": "aspnetcore/fundamentals/minimal-apis/overview.md",
1644+
"redirect_url": "/aspnet/core/fundamentals/apis",
1645+
"redirect_document_id": false
1646+
},
1647+
{
1648+
"source_path": "aspnetcore/web-api/handle-errors.md",
1649+
"redirect_url": "/aspnet/core/fundamentals/error-handling-api",
1650+
"redirect_document_id": false
1651+
},
1652+
{
1653+
"source_path": "aspnetcore/fundamentals/handle-errors.md",
1654+
"redirect_url": "/aspnet/core/fundamentals/error-handling-api",
1655+
"redirect_document_id": false
16311656
}
16321657
]
16331658
}

aspnetcore/blazor/forms/validation.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -597,7 +597,7 @@ app.MapDefaultControllerRoute();
597597
For more information on controller routing and validation failure error responses, see the following resources:
598598

599599
* <xref:mvc/controllers/routing>
600-
* <xref:web-api/handle-errors#validation-failure-error-response>
600+
* <xref:fundamentals/error-handling-api#validation-failure-error-response>
601601

602602
In the `.Client` project, add the `CustomValidation` component shown in the [Validator components](#validator-components) section. Update the namespace to match the app (for example, `namespace BlazorSample.Client`).
603603

aspnetcore/blazor/project-structure.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -642,7 +642,7 @@ To create an app that can run as either a Blazor Server app or a Blazor WebAssem
642642

643643
* <xref:blazor/tooling>
644644
* <xref:blazor/hosting-models>
645-
* <xref:fundamentals/minimal-apis/overview>
645+
* <xref:fundamentals/apis>
646646
* [Blazor samples GitHub repository (`dotnet/blazor-samples`)](https://github.com/dotnet/blazor-samples) ([how to download](xref:blazor/fundamentals/index#sample-apps))
647647

648648
:::moniker-end

aspnetcore/blazor/security/webassembly/standalone-with-identity/index.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ Services and endpoints for [Swagger/OpenAPI](xref:tutorials/web-api-help-pages-u
162162

163163
:::moniker-end
164164

165-
User role claims are sent from a [Minimal API](xref:fundamentals/minimal-apis/overview) at the `/roles` endpoint.
165+
User role claims are sent from a [Minimal API](xref:fundamentals/apis) at the `/roles` endpoint.
166166

167167
Routes are mapped for Identity endpoints by calling `MapIdentityApi<AppUser>()`.
168168

@@ -252,7 +252,7 @@ Role claims aren't sent back from the `manage/info` endpoint to create user clai
252252
253253
In the `CookieAuthenticationStateProvider`, a roles request is made to the `/roles` endpoint of the `Backend` server API project. The response is read into a string by calling <xref:System.Net.Http.HttpContent.ReadAsStringAsync>. <xref:System.Text.Json.JsonSerializer.Deserialize%2A?displayProperty=nameWithType> deserializes the string into a custom `RoleClaim` array. Finally, the claims are added to the user's claims collection.
254254
255-
In the `Backend` server API's `Program` file, a [Minimal API](xref:fundamentals/minimal-apis/overview) manages the `/roles` endpoint. Claims of <xref:System.Security.Claims.ClaimsIdentity.RoleClaimType%2A> are [selected](xref:System.Linq.Enumerable.Select%2A) into an [anonymous type](/dotnet/csharp/fundamentals/types/anonymous-types) and serialized for return to the `BlazorWasmAuth` project with <xref:Microsoft.AspNetCore.Http.TypedResults.Json%2A?displayProperty=nameWithType>.
255+
In the `Backend` server API's `Program` file, a [Minimal API](xref:fundamentals/apis) manages the `/roles` endpoint. Claims of <xref:System.Security.Claims.ClaimsIdentity.RoleClaimType%2A> are [selected](xref:System.Linq.Enumerable.Select%2A) into an [anonymous type](/dotnet/csharp/fundamentals/types/anonymous-types) and serialized for return to the `BlazorWasmAuth` project with <xref:Microsoft.AspNetCore.Http.TypedResults.Json%2A?displayProperty=nameWithType>.
256256
257257
The roles endpoint requires authorization by calling <xref:Microsoft.AspNetCore.Builder.AuthorizationEndpointConventionBuilderExtensions.RequireAuthorization%2A>. If you decide not to use Minimal APIs in favor of controllers for secure server API endpoints, be sure to set the [`[Authorize]` attribute](xref:Microsoft.AspNetCore.Authorization.AuthorizeAttribute) on controllers or actions.
258258

aspnetcore/fundamentals/apis.md

Lines changed: 132 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,53 +1,164 @@
11
---
2-
title: "APIs overview"
3-
author: tdykstra
4-
description: Learn about differences between controller-based APIs and minimal APIs.
5-
ms.author: tdykstra
6-
ms.date: 4/10/2023
2+
title: APIs overview
3+
author: JeremyLikness
4+
description: Learn how to build fast HTTP APIs with ASP.NET Core using Minimal APIs, the recommended approach for new projects.
5+
ai-usage: ai-assisted
6+
ms.author: jeliknes
7+
ms.date: 08/15/2025
78
monikerRange: '>= aspnetcore-6.0'
89
uid: fundamentals/apis
910
---
1011

11-
# Choose between controller-based APIs and minimal APIs
12+
# APIs overview
1213

1314
[!INCLUDE[](~/includes/not-latest-version.md)]
1415

1516
:::moniker range=">= aspnetcore-7.0"
1617

17-
ASP.NET Core supports two approaches to creating APIs: a controller-based approach and minimal APIs. *Controllers* in an API project are classes that derive from <xref:Microsoft.AspNetCore.Mvc.ControllerBase>. *Minimal APIs* define endpoints with logical handlers in lambdas or methods. This article points out differences between the two approaches.
18+
ASP.NET Core provides two approaches for building HTTP APIs: **Minimal APIs** and controller-based APIs. **For new projects, we recommend using Minimal APIs** as they provide a simplified, high-performance approach for building APIs with minimal code and configuration.
1819

19-
The design of minimal APIs hides the host class by default and focuses on configuration and extensibility via extension methods that take functions as lambda expressions. Controllers are classes that can take dependencies via constructor injection or property injection, and generally follow object-oriented patterns. Minimal APIs support dependency injection through other approaches such as accessing the service provider.
20+
## Minimal APIs - Recommended for new projects
21+
22+
Minimal APIs are the recommended approach for building fast HTTP APIs with ASP.NET Core. They allow you to build fully functioning REST endpoints with minimal code and configuration. Skip traditional scaffolding and avoid unnecessary controllers by fluently declaring API routes and actions.
23+
24+
Here's a simple example that creates an API at the root of the web app:
25+
26+
```csharp
27+
var app = WebApplication.Create(args);
28+
29+
app.MapGet("/", () => "Hello World!");
30+
31+
app.Run();
32+
```
33+
34+
Most APIs accept parameters as part of the route:
35+
36+
```csharp
37+
var builder = WebApplication.CreateBuilder(args);
38+
39+
var app = builder.Build();
40+
41+
app.MapGet("/users/{userId}/books/{bookId}",
42+
(int userId, int bookId) => $"The user id is {userId} and book id is {bookId}");
43+
44+
app.Run();
45+
```
46+
47+
Minimal APIs support the configuration and customization needed to scale to multiple APIs, handle complex routes, apply authorization rules, and control the content of API responses.
48+
49+
### Getting started with Minimal APIs
50+
51+
* **Tutorial**: <xref:tutorials/min-web-api>
52+
* **Quick reference**: <xref:fundamentals/minimal-apis>
53+
* **Examples**: For a full list of common scenarios with code examples, see <xref:fundamentals/minimal-apis>
54+
55+
## Controller-based APIs - Alternative approach
56+
57+
ASP.NET Core also supports a controller-based approach where controllers are classes that derive from <xref:Microsoft.AspNetCore.Mvc.ControllerBase>. This approach follows traditional object-oriented patterns and may be preferred for:
58+
59+
* Large applications with complex business logic
60+
* Teams familiar with the MVC pattern
61+
* Applications requiring specific MVC features
2062

2163
Here's sample code for an API based on controllers:
2264

2365
:::code language="csharp" source="~/fundamentals/apis/APIWithControllers/Program.cs":::
2466

2567
:::code language="csharp" source="~/fundamentals/apis/APIWithControllers/Controllers/WeatherForecastController.cs":::
2668

27-
The following code provides the same functionality in a minimal API project. Notice that the minimal API approach involves including the related code in lambda expressions.
69+
The following code provides the same functionality using the recommended Minimal API approach:
2870

2971
:::code language="csharp" source="~/fundamentals/apis/MinimalAPI/Program.cs":::
3072

3173
Both API projects refer to the following class:
3274

3375
:::code language="csharp" source="~/fundamentals/apis/APIWithControllers/WeatherForecast.cs":::
3476

35-
Minimal APIs have many of the same capabilities as controller-based APIs. They support the configuration and customization needed to scale to multiple APIs, handle complex routes, apply authorization rules, and control the content of API responses. There are a few capabilities available with controller-based APIs that are not yet supported or implemented by minimal APIs. These include:
77+
## Choosing between approaches
78+
79+
**Start with Minimal APIs** for new projects. They offer:
80+
81+
* **Simpler syntax** - Less boilerplate code
82+
* **Better performance** - Reduced overhead compared to controllers
83+
* **Easier testing** - Simplified unit and integration testing
84+
* **Modern approach** - Leverages the latest .NET features
85+
86+
**Consider controller-based APIs** if you need:
87+
88+
* Model binding extensibility (<xref:Microsoft.AspNetCore.Mvc.ModelBinding.IModelBinderProvider>, <xref:Microsoft.AspNetCore.Mvc.ModelBinding.IModelBinder>)
89+
* Advanced validation features (<xref:Microsoft.AspNetCore.Mvc.ModelBinding.Validation.IModelValidator>)
90+
* [Application parts](xref:mvc/extensibility/app-parts) or the [application model](xref:mvc/controllers/application-model)
91+
* [JsonPatch](https://www.nuget.org/packages/Microsoft.AspNetCore.JsonPatch/) support
92+
* [OData](https://www.nuget.org/packages/Microsoft.AspNetCore.OData/) support
3693

37-
* No built-in support for model binding (<xref:Microsoft.AspNetCore.Mvc.ModelBinding.IModelBinderProvider>, <xref:Microsoft.AspNetCore.Mvc.ModelBinding.IModelBinder>). Support can be added with a custom binding shim.
38-
* No built-in support for validation (<xref:Microsoft.AspNetCore.Mvc.ModelBinding.Validation.IModelValidator>).
39-
* No support for [application parts](xref:mvc/extensibility/app-parts) or the [application model](xref:mvc/controllers/application-model). There's no way to apply or build your own conventions.
40-
* No built-in view rendering support. We recommend using [Razor Pages](xref:tutorials/razor-pages/razor-pages-start) for rendering views.
41-
* No support for [JsonPatch](https://www.nuget.org/packages/Microsoft.AspNetCore.JsonPatch/)
42-
* No support for [OData](https://www.nuget.org/packages/Microsoft.AspNetCore.OData/)
94+
Most of these features can be implemented in Minimal APIs with custom solutions, but controllers provide them out of the box.
4395

4496
## See also
4597

46-
* <xref:web-api/index>.
47-
* <xref:tutorials/first-web-api>
48-
* <xref:fundamentals/minimal-apis/overview>
49-
* <xref:tutorials/min-web-api>
98+
* <xref:tutorials/min-web-api> - Minimal API tutorial
99+
* <xref:fundamentals/minimal-apis> - Minimal APIs quick reference
100+
* <xref:web-api/index> - Controller-based APIs overview
101+
* <xref:tutorials/first-web-api> - Controller-based API tutorial
50102

51103
:::moniker-end
52104

53-
[!INCLUDE[](~/fundamentals/includes/apis6.md)]
105+
:::moniker range="= aspnetcore-6.0"
106+
107+
ASP.NET Core provides two approaches for building HTTP APIs: **Minimal APIs** and controller-based APIs. **For new projects, we recommend using Minimal APIs** as they provide a simplified, high-performance approach for building APIs with minimal code and configuration.
108+
109+
## Minimal APIs - Recommended for new projects
110+
111+
Minimal APIs are the recommended approach for building fast HTTP APIs with ASP.NET Core. They allow you to build fully functioning REST endpoints with minimal code and configuration.
112+
113+
Here's a simple example:
114+
115+
```csharp
116+
var app = WebApplication.Create(args);
117+
118+
app.MapGet("/", () => "Hello World!");
119+
120+
app.Run();
121+
```
122+
123+
### Getting started with Minimal APIs
124+
125+
* **Tutorial**: <xref:tutorials/min-web-api>
126+
* **Quick reference**: <xref:fundamentals/minimal-apis>
127+
128+
## Controller-based APIs - Alternative approach
129+
130+
Controllers are classes that derive from <xref:Microsoft.AspNetCore.Mvc.ControllerBase>. This approach follows traditional object-oriented patterns.
131+
132+
Here's sample code for an API based on controllers:
133+
134+
:::code language="csharp" source="~/fundamentals/apis/APIWithControllers/Program.cs":::
135+
136+
:::code language="csharp" source="~/fundamentals/apis/APIWithControllers/Controllers/WeatherForecastController.cs":::
137+
138+
The following code provides the same functionality using the recommended Minimal API approach:
139+
140+
:::code language="csharp" source="~/fundamentals/apis/MinimalAPI/Program.cs":::
141+
142+
Both API projects refer to the following class:
143+
144+
:::code language="csharp" source="~/fundamentals/apis/APIWithControllers/WeatherForecast.cs":::
145+
146+
## Choosing between approaches
147+
148+
**Start with Minimal APIs** for new projects. Consider controller-based APIs if you need:
149+
150+
* Model binding extensibility (<xref:Microsoft.AspNetCore.Mvc.ModelBinding.IModelBinderProvider>, <xref:Microsoft.AspNetCore.Mvc.ModelBinding.IModelBinder>)
151+
* Form binding support, including <xref:Microsoft.AspNetCore.Http.IFormFile>
152+
* Advanced validation features (<xref:Microsoft.AspNetCore.Mvc.ModelBinding.Validation.IModelValidator>)
153+
* [Application parts](xref:mvc/extensibility/app-parts) or the [application model](xref:mvc/controllers/application-model)
154+
* [JsonPatch](https://www.nuget.org/packages/Microsoft.AspNetCore.JsonPatch/) support
155+
* [OData](https://www.nuget.org/packages/Microsoft.AspNetCore.OData/) support
156+
157+
## See also
158+
159+
* <xref:tutorials/min-web-api> - Minimal API tutorial
160+
* <xref:fundamentals/minimal-apis> - Minimal APIs quick reference
161+
* <xref:web-api/index> - Controller-based APIs overview
162+
* <xref:tutorials/first-web-api> - Controller-based API tutorial
163+
164+
:::moniker-end

0 commit comments

Comments
 (0)