Skip to content

[Breaking change]: Deprecation of IActionContextAccessor #520

@captainsafia

Description

@captainsafia

Description

IActionContextAccessor and ActionContextAccessor have been marked as obsolete in ASP.NET Core 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().

Version

.NET 10 Preview 7

Previous behavior

Developers could use IActionContextAccessor to access the current ActionContext:

public class MyService
{
   private readonly IActionContextAccessor _actionContextAccessor;
   
   public MyService(IActionContextAccessor actionContextAccessor)
   {
       _actionContextAccessor = actionContextAccessor;
   }
   
   public void DoSomething()
   {
       var actionContext = _actionContextAccessor.ActionContext;
       var actionDescriptor = actionContext?.ActionDescriptor;
       // Use action descriptor metadata
   }
}

New behavior

Using IActionContextAccessor and ActionContextAccessor will produce a compiler warning with diagnostic ID ASPDEPR006:

warning ASPDEPR006: ActionContextAccessor is obsolete and will be removed in a future version. For more information, visit https://aka.ms/aspnet/deprecate/006.

Type of breaking change

  • Source incompatible: When recompiled using the new SDK, existing source code will receive obsolete warnings and may require source changes for future compatibility.
  • Behavioral change: The functionality still works but is marked for future removal.

Reason for change

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.

Recommended action

Migrate from IActionContextAccessor to IHttpContextAccessor and use HttpContext.GetEndpoint():

Before:

public class MyService
{
   private readonly IActionContextAccessor _actionContextAccessor;
   
   public MyService(IActionContextAccessor actionContextAccessor)
   {
       _actionContextAccessor = actionContextAccessor;
   }
   
   public void DoSomething()
   {
       var actionContext = _actionContextAccessor.ActionContext;
       var actionDescriptor = actionContext?.ActionDescriptor;
       // Use action descriptor metadata
   }
}

After:

public class MyService
{
   private readonly IHttpContextAccessor _httpContextAccessor;
   
   public MyService(IHttpContextAccessor httpContextAccessor)
   {
       _httpContextAccessor = httpContextAccessor;
   }
   
   public void DoSomething()
   {
       var httpContext = _httpContextAccessor.HttpContext;
       var endpoint = httpContext?.GetEndpoint();
       var actionDescriptor = endpoint?.Metadata.GetMetadata<ActionDescriptor>();
       // Use action descriptor metadata
   }
}

Affected APIs

  • Microsoft.AspNetCore.Mvc.Infrastructure.IActionContextAccessor
  • Microsoft.AspNetCore.Mvc.Infrastructure.ActionContextAccessor

Metadata

Metadata

Assignees

No one assigned

    Labels

    Breaking changeDocumentedThe breaking change has been published to the .NET Core docs

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions