|
| 1 | +--- |
| 2 | +title: "IActionContextAccessor and ActionContextAccessor are obsolete" |
| 3 | +description: "Learn about the breaking change in ASP.NET Core 10 where IActionContextAccessor and ActionContextAccessor are marked as obsolete." |
| 4 | +ms.date: 08/07/2025 |
| 5 | +ai-usage: ai-assisted |
| 6 | +ms.custom: https://github.com/aspnet/Announcements/issues/520 |
| 7 | +--- |
| 8 | + |
| 9 | +# IActionContextAccessor and ActionContextAccessor are obsolete |
| 10 | + |
| 11 | +<xref:Microsoft.AspNetCore.Mvc.Infrastructure.IActionContextAccessor> and <xref:Microsoft.AspNetCore.Mvc.Infrastructure.ActionContextAccessor> have been marked as obsolete with diagnostic ID `ASPDEPR006`. With the introduction of endpoint routing, `IActionContextAccessor` is no longer necessary as developers can access action descriptor and metadata information directly through `HttpContext.GetEndpoint()`. |
| 12 | + |
| 13 | +## Version introduced |
| 14 | + |
| 15 | +.NET 10 Preview 7 |
| 16 | + |
| 17 | +## Previous behavior |
| 18 | + |
| 19 | +Previously, you could use `IActionContextAccessor` to access the current <xref:Microsoft.AspNetCore.Mvc.ActionContext>: |
| 20 | + |
| 21 | +```csharp |
| 22 | +public class MyService |
| 23 | +{ |
| 24 | + private readonly IActionContextAccessor _actionContextAccessor; |
| 25 | + |
| 26 | + public MyService(IActionContextAccessor actionContextAccessor) |
| 27 | + { |
| 28 | + _actionContextAccessor = actionContextAccessor; |
| 29 | + } |
| 30 | + |
| 31 | + public void DoSomething() |
| 32 | + { |
| 33 | + var actionContext = _actionContextAccessor.ActionContext; |
| 34 | + var actionDescriptor = actionContext?.ActionDescriptor; |
| 35 | + // Use action descriptor metadata. |
| 36 | + } |
| 37 | +} |
| 38 | +``` |
| 39 | + |
| 40 | +## New behavior |
| 41 | + |
| 42 | +Starting in .NET 10, using `IActionContextAccessor` and `ActionContextAccessor` produces a compiler warning with diagnostic ID `ASPDEPR006`: |
| 43 | + |
| 44 | +> warning ASPDEPR006: ActionContextAccessor is obsolete and will be removed in a future version. For more information, visit <https://aka.ms/aspnet/deprecate/006>. |
| 45 | +
|
| 46 | +## Type of breaking change |
| 47 | + |
| 48 | +This change can affect [source compatibility](../../categories.md#source-compatibility). |
| 49 | + |
| 50 | +## Reason for change |
| 51 | + |
| 52 | +With the introduction of endpoint routing in ASP.NET Core, `IActionContextAccessor` is no longer necessary. The endpoint routing infrastructure provides a cleaner, more direct way to access endpoint metadata through `HttpContext.GetEndpoint()`, aligning with ASP.NET Core's architectural evolution toward endpoint routing. |
| 53 | + |
| 54 | +## Recommended action |
| 55 | + |
| 56 | +Migrate from `IActionContextAccessor` to <xref:Microsoft.AspNetCore.Http.IHttpContextAccessor> and use `HttpContext.GetEndpoint()`: |
| 57 | + |
| 58 | +Before: |
| 59 | + |
| 60 | +```csharp |
| 61 | +public class MyService |
| 62 | +{ |
| 63 | + private readonly IActionContextAccessor _actionContextAccessor; |
| 64 | + |
| 65 | + public MyService(IActionContextAccessor actionContextAccessor) |
| 66 | + { |
| 67 | + _actionContextAccessor = actionContextAccessor; |
| 68 | + } |
| 69 | + |
| 70 | + public void DoSomething() |
| 71 | + { |
| 72 | + var actionContext = _actionContextAccessor.ActionContext; |
| 73 | + var actionDescriptor = actionContext?.ActionDescriptor; |
| 74 | + // Use action descriptor metadata |
| 75 | + } |
| 76 | +} |
| 77 | +``` |
| 78 | + |
| 79 | +After: |
| 80 | + |
| 81 | +```csharp |
| 82 | +public class MyService |
| 83 | +{ |
| 84 | + private readonly IHttpContextAccessor _httpContextAccessor; |
| 85 | + |
| 86 | + public MyService(IHttpContextAccessor httpContextAccessor) |
| 87 | + { |
| 88 | + _httpContextAccessor = httpContextAccessor; |
| 89 | + } |
| 90 | + |
| 91 | + public void DoSomething() |
| 92 | + { |
| 93 | + var httpContext = _httpContextAccessor.HttpContext; |
| 94 | + var endpoint = httpContext?.GetEndpoint(); |
| 95 | + var actionDescriptor = endpoint?.Metadata.GetMetadata<ActionDescriptor>(); |
| 96 | + // Use action descriptor metadata. |
| 97 | + } |
| 98 | +} |
| 99 | +``` |
| 100 | + |
| 101 | +## Affected APIs |
| 102 | + |
| 103 | +- <xref:Microsoft.AspNetCore.Mvc.Infrastructure.IActionContextAccessor?displayProperty=fullName> |
| 104 | +- <xref:Microsoft.AspNetCore.Mvc.Infrastructure.ActionContextAccessor?displayProperty=fullName> |
0 commit comments