1

In ASP.NET MVC Core 2.0 you can inject the ActionContext, into services, using the IActionContextAccessor interface.

I would like to do the same in Razor Pages but there isn't (from what I can see) an IPageContextAccessor.

Doe anyone know how to inject the PageContext into a service?

tereško
  • 58,060
  • 25
  • 98
  • 150
Steven Bey
  • 31
  • 3

1 Answers1

1

The IActionContextAccessor does work in Razor Pages. The reason it wasn't working was because I hadn't registered the IActionContextAccessor service[1]:

services.AddSingleton<IActionContextAccessor, ActionContextAccessor>();

Note: it must be registered as a Singleton.

[1] https://github.com/aspnet/Mvc/issues/3936#issuecomment-194367440

Steven Bey
  • 31
  • 3