Skip to content

IUrlHelper depends on ActionContext obsoleted in ASP.NET Core 10 #64361

@timstokman

Description

@timstokman

Is there an existing issue for this?

  • I have searched the existing issues

Describe the bug

For asp .net core 9 and lower, you could inject the url helper like this:

services.AddScoped<IUrlHelper>(x =>
{
   var actionContext = x.GetRequiredService<IActionContextAccessor>().ActionContext;
   var factory = x.GetRequiredService<IUrlHelperFactory>();
   return factory.GetUrlHelper(actionContext);
});

ActionContextAccessor is now obsolete, in the future that would probably mean there'd be no way to get an actioncontext (and also no way to get the url helper).

My suggestions would be:

  • Make sure you can construct url helpers with the new EndpointMetadataCollection
  • Make the url helper available as a scoped service by default in the dependency injection

Expected Behavior

For a service not to depend on obsoleted classes, for there to be a supported way of using IUrlHelper

Steps To Reproduce

The traditional way of instantiating IUrlHelper is now giving warnings, and in the future will be impossible:

services.AddScoped<IUrlHelper>(x =>
{
   var actionContext = x.GetRequiredService<IActionContextAccessor>().ActionContext;
   var factory = x.GetRequiredService<IUrlHelperFactory>();
   return factory.GetUrlHelper(actionContext);
});

Exceptions (if any)

No response

.NET Version

10.0.10

Anything else?

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    area-mvcIncludes: MVC, Actions and Controllers, Localization, CORS, most templates

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions