|
| 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: 01/08/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 | +`IActionContextAccessor` and `ActionContextAccessor` have been marked as obsolete in ASP.NET Core 10 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 | +Developers could use `IActionContextAccessor` to access the current `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 | +Using `IActionContextAccessor` and `ActionContextAccessor` produces a compiler warning with diagnostic ID `ASPDEPR006`: |
| 43 | + |
| 44 | +``` |
| 45 | +warning ASPDEPR006: ActionContextAccessor is obsolete and will be removed in a future version. For more information, visit https://aka.ms/aspnet/deprecate/006. |
| 46 | +``` |
| 47 | + |
| 48 | +## Type of breaking change |
| 49 | + |
| 50 | +This change can affect [source compatibility](../../categories.md#source-compatibility) and is a [behavioral change](../../categories.md#behavioral-change). |
| 51 | + |
| 52 | +## Reason for change |
| 53 | + |
| 54 | +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. |
| 55 | + |
| 56 | +## Recommended action |
| 57 | + |
| 58 | +Migrate from `IActionContextAccessor` to `IHttpContextAccessor` and use `HttpContext.GetEndpoint()`: |
| 59 | + |
| 60 | +**Before:** |
| 61 | + |
| 62 | +```csharp |
| 63 | +public class MyService |
| 64 | +{ |
| 65 | + private readonly IActionContextAccessor _actionContextAccessor; |
| 66 | + |
| 67 | + public MyService(IActionContextAccessor actionContextAccessor) |
| 68 | + { |
| 69 | + _actionContextAccessor = actionContextAccessor; |
| 70 | + } |
| 71 | + |
| 72 | + public void DoSomething() |
| 73 | + { |
| 74 | + var actionContext = _actionContextAccessor.ActionContext; |
| 75 | + var actionDescriptor = actionContext?.ActionDescriptor; |
| 76 | + // Use action descriptor metadata |
| 77 | + } |
| 78 | +} |
| 79 | +``` |
| 80 | + |
| 81 | +**After:** |
| 82 | + |
| 83 | +```csharp |
| 84 | +public class MyService |
| 85 | +{ |
| 86 | + private readonly IHttpContextAccessor _httpContextAccessor; |
| 87 | + |
| 88 | + public MyService(IHttpContextAccessor httpContextAccessor) |
| 89 | + { |
| 90 | + _httpContextAccessor = httpContextAccessor; |
| 91 | + } |
| 92 | + |
| 93 | + public void DoSomething() |
| 94 | + { |
| 95 | + var httpContext = _httpContextAccessor.HttpContext; |
| 96 | + var endpoint = httpContext?.GetEndpoint(); |
| 97 | + var actionDescriptor = endpoint?.Metadata.GetMetadata<ActionDescriptor>(); |
| 98 | + // Use action descriptor metadata |
| 99 | + } |
| 100 | +} |
| 101 | +``` |
| 102 | + |
| 103 | +## Affected APIs |
| 104 | + |
| 105 | +- <xref:Microsoft.AspNetCore.Mvc.Infrastructure.IActionContextAccessor?displayProperty=fullName> |
| 106 | +- <xref:Microsoft.AspNetCore.Mvc.Infrastructure.ActionContextAccessor?displayProperty=fullName> |
0 commit comments