Skip to content

Commit 3fa8f34

Browse files
Copilotgewarren
andcommitted
Document IActionContextAccessor deprecation breaking change for .NET 10
Co-authored-by: gewarren <[email protected]>
1 parent 7a0e3cc commit 3fa8f34

File tree

3 files changed

+116
-0
lines changed

3 files changed

+116
-0
lines changed

docs/core/compatibility/10.0.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,12 @@ If you're migrating an app to .NET 10, the breaking changes listed here might af
1515
> [!NOTE]
1616
> This article is a work in progress. It's not a complete list of breaking changes in .NET 10.
1717
18+
## ASP.NET Core
19+
20+
| Title | Type of change | Introduced version |
21+
|-------|-------------------|--------------------|
22+
| [IActionContextAccessor and ActionContextAccessor are obsolete](aspnet-core/10.0/iactioncontextaccessor-obsolete.md) | Source incompatible/behavioral change | Preview 7 |
23+
1824
## Containers
1925

2026
| Title | Type of change | Introduced version |
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
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>

docs/core/compatibility/toc.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@ items:
88
items:
99
- name: Overview
1010
href: 10.0.md
11+
- name: ASP.NET Core
12+
items:
13+
- name: IActionContextAccessor and ActionContextAccessor are obsolete
14+
href: aspnet-core/10.0/iactioncontextaccessor-obsolete.md
1115
- name: Containers
1216
items:
1317
- name: Default .NET images use Ubuntu

0 commit comments

Comments
 (0)