diff --git a/aspnetcore/mvc/controllers/application-model.md b/aspnetcore/mvc/controllers/application-model.md index 2c5ff9b802e2..c29bd130fd00 100644 --- a/aspnetcore/mvc/controllers/application-model.md +++ b/aspnetcore/mvc/controllers/application-model.md @@ -3,7 +3,7 @@ title: Work with the application model in ASP.NET Core author: tdykstra description: Learn how to read and manipulate the application model to modify how MVC elements behave in ASP.NET Core. ms.author: tdykstra -ms.date: 04/05/2021 +ms.date: 09/06/2026 uid: mvc/controllers/application-model --- # Work with the application model in ASP.NET Core @@ -217,3 +217,66 @@ The application model exposes an directly. For more information, see . + +:::moniker-end + +:::moniker range=">= aspnetcore-6.0 < aspnetcore-9.0" + +> [!NOTE] +> is an advanced extensibility point intended for framework and library authors. Most apps don't need to implement it. Starting with .NET 9, use the built-in OpenAPI document, operation, and schema transformers to customize generated API documentation. For more information, see . + +ASP.NET Core uses implementations to discover endpoints and generate metadata. Tools such as Swashbuckle and NSwag inspect these `ApiDescription` instances when producing API documentation. + +Implement to programmatically inspect or modify `ApiDescription` instances produced by the framework: + +* : Executes in ascending order of the property to construct `ApiDescription` metadata for discovered endpoints. +* : Executes in reverse order after all providers have executed, allowing customization or enrichment of generated `ApiDescription` instances. + +The following example demonstrates a custom `IApiDescriptionProvider` that adds custom metadata properties to discovered API descriptions: + +```csharp +using Microsoft.AspNetCore.Mvc.ApiExplorer; + +public class CustomApiDescriptionProvider : IApiDescriptionProvider +{ + // Execute after the framework's default ApiDescriptionProvider (Order = -1000) + public int Order => 0; + + public void OnProvidersExecuting(ApiDescriptionProviderContext context) + { + // No action required during initial execution phase + } + + public void OnProvidersExecuted(ApiDescriptionProviderContext context) + { + foreach (var apiDescription in context.Results) + { + // Enrich or modify ApiDescription metadata + apiDescription.Properties["CustomMetadata"] = "CustomValue"; + } + } +} +``` + +Register the custom provider with dependency injection using in `Program.cs`: + +```csharp +using Microsoft.AspNetCore.Mvc.ApiExplorer; +using Microsoft.Extensions.DependencyInjection.Extensions; + +var builder = WebApplication.CreateBuilder(args); + +builder.Services.AddControllers(); +builder.Services.TryAddEnumerable( + ServiceDescriptor.Transient()); + +var app = builder.Build(); +``` + +:::moniker-end