In RC1, IUrlHelper
could be injected in services (with services.AddMvc()
in startup class)
This doesn\'t work anymore in <
For ASP.NET Core 3.x app just inject IHttpContextAccessor
and LinkGenerator to your controller or service. They should be already available in DI
.
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Routing;
namespace Coding-Machine.NET
{
public class MyService
{
private readonly IHttpContextAccessor _accessor;
private readonly LinkGenerator _generator;
public MyService(IHttpContextAccessor accessor, LinkGenerator generator)
{
_accessor = accessor;
_generator = generator;
}
private string GenerateConfirmEmailLink()
{
var callbackLink = _generator.GetUriByPage(_accessor.HttpContext,
page: "/Account/ConfirmEmail",
handler: null,
values: new {area = "Identity", userId = 123, code = "ASDF1234"});
return callbackLink;
}
}
}
If your app can't resolve IHttpContextAccessor
just add this to DI
:
public void ConfigureServices(IServiceCollection services)
{
services.AddHttpContextAccessor();
}