asp.net-core - LinkGenerator GetPathByPage 不返回完整的 Uri
问题描述
我在 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});
解决方案
按照设计linkGenerator.GetPathByPage
将返回相对路径。如果您需要完整Uri
包含Scheme
,Host
那么您必须使用另一种方法GetUriByPage
,LinkGenerator
如下所示:
var callbackUrl = linkGenerator.GetUriByPage(httpContextAccessor.HttpContext,
"/Account/Login", null, new {area = "Identity", userId = user.Id});
推荐阅读
- docker - 在 Container-Optimized OS 上的 docker 中设置端口映射
- docker - 无效的安装路径:“[”安装路径必须是绝对的
- jmeter - 通过blazemeter运行时如何在结果中记录正则表达式提取器值
- windows - Windows 替代 macOS os_signpost 函数——在 Intel VTune 或 VS Profiler 中发布自定义标记
- c - 并行操作
- node.js - 如何在 express 应用中正确使用 socket.io
- vb.net - 如何在 VB.script 的 html 助手中添加 aria-label
- python - 如何从 pandas 读取动态 json 到 pyarrow.Table
- python - python - 将 SQLAlchemy ORM 的 @hybrid_property 加载到 pandas DataFrame
- sql-server - 自动增量值