asp.net-core - 如何在自定义 TagHelper 中生成 Razor 页面 url
问题描述
我有一个自定义标签助手,它应该呈现如下内容:
<ol>
<li><a href="/my/razor/page/url">Some text</a>
</ol>
如果我要在 Razor 页面中执行此操作,我只需执行以下操作:<a asp-page="MyRazorPage">Some text</a>
有没有办法在 TagHelper 内部做类似的事情?
解决方案
I found the answer.
Inject IUrlHelperFactory
into the constructor as well as use the following property:
[HtmlAttributeNotBound]
[ViewContext]
public ViewContext ViewContext { get; set; }
Then you can create an IUrlHelper
this way:
var urlHelper = _urlHelperFactory.GetUrlHelper(ViewContext);
var url = urlHelper.Page("/Clients/Edit", new { Id = myClientId });
output.Content.AppendHtmlLine($"<a href='{url}'>Edit</a>");
推荐阅读
- javascript - 如何使用参数化输入将 NULL 值从 nodeJS/javascript 插入 MySQL?
- android - android - 我的 SwitchCompat 无法转换为 SwitchCompat
- audio - 如何使用 ffmpeg 将单个 showwaves(或 showfreqs)叠加层与两个对称滚动的 showspectrum 叠加层集中定位?
- spring-boot - 骆驼拆分/聚合和合并包含列表的列表包含列表的列表
- c - 分段错误 11 尝试使用 memcpy 后获取数据
- python - 压缩神经网络的输入特征向量
- c++ - 写出在子类中创建为指针的数组(C++)
- asp.net-core - 如何将所有消息从 System.Diagnostics.Trace 传输到 ILogger?
- java - 在 NXT Brick 上读取上传的文件
- php - 安装 grav localhost debian