IActionContextAccessor och ActionContextAccessor är föråldrade

IActionContextAccessor och ActionContextAccessor har markerats som föråldrad med diagnostik-ID ASPDEPR006. Med introduktionen av slutpunktsroutning IActionContextAccessor behövs inte längre eftersom utvecklare kan komma åt åtgärdsbeskrivnings- och metadatainformation direkt via HttpContext.GetEndpoint().

Version lanserad

.NET 10 Förhandsversion 7

Tidigare beteende

Tidigare kunde du använda IActionContextAccessor för att komma åt den aktuella 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.
   }
}

Nytt beteende

Från och med .NET 10 använder IActionContextAccessor och ActionContextAccessor skapar du en kompilatorvarning med diagnostik-ID ASPDEPR006:

varning ASPDEPR006: ActionContextAccessor är föråldrad och tas bort i en framtida version. Mer information finns på https://aka.ms/aspnet/deprecate/006.

Typ av brytande ändring

Den här ändringen kan påverka källkompatibilitet.

Orsak till ändring

Med introduktionen av slutpunktsroutning i ASP.NET Core IActionContextAccessor är det inte längre nödvändigt. Infrastrukturen för slutpunktsroutning ger ett renare och mer direkt sätt att komma åt slutpunktsmetadata via HttpContext.GetEndpoint(), i linje med ASP.NET Cores arkitekturutveckling mot slutpunktsroutning.

Migrera från IActionContextAccessor till IHttpContextAccessor och använd HttpContext.GetEndpoint():

Före:

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
   }
}

Efter:

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.
   }
}

Berörda API:er