LinkGenerator GetPathByPage 不返回完整的 Uri

Joe*_*mer 4 asp.net-core-mvc asp.net-core

我在 ASP.NET Core 2.2 MVC 项目中的 EmailSender 服务中使用 LinkGenerator。它首先返回 Null 值,直到我发现我需要在定义中包含该区域。但是,Url 仍然没有提供我期望的服务器信息。

它生成的 URL 如下所示: "/Identity/Account/Login...

我期待这个: "https://{hostName}/Identity/Account/Login..."

我的解决方法是将 httpContextAccessor 中发布的值连接起来。但是,这似乎是假的。有人可以给我指示这应该如何工作吗?

解决(不是很好):

var callbackUrl = $"{httpContextAccessor.HttpContext.Request.Scheme}://" +
                  $"{httpContextAccessor.HttpContext.Request.Host}" +
                  linkGenerator.GetPathByPage(httpContextAccessor.HttpContext,
                            "/Account/Login", null, new {area = "Identity", userId = user.Id});
Run Code Online (Sandbox Code Playgroud)

Tan*_*jel 6

通过设计linkGenerator.GetPathByPage将返回相关途径了,你需要完整的Uri,包括SchemeHost,那么你必须使用另一种方法GetUriByPage还提供LinkGenerator如下:

var callbackUrl = linkGenerator.GetUriByPage(httpContextAccessor.HttpContext,
    "/Account/Login", null, new {area = "Identity", userId = user.Id});
Run Code Online (Sandbox Code Playgroud)