【问题标题】:Trying to generate clickable telephone link in .net Core 5 Razor pages project尝试在.net Core 5 Razor pages 项目中生成可点击的电话链接
【发布时间】:2021-07-21 23:02:56
【问题描述】:

我目前正在生成员工列表和联系人列表页面,需要将电话号码格式化为

 <a href="tel: +123456789"> 123456789 </a>

我可以手动执行此操作,但考虑到这将在整个站点中再次发生,我认为 HTML 扩展方法将是实现它的最佳方式。

我找到了这个链接Display HTML 5 Telephone number link in Razor

但是提供的两种解决方案无法编译,我无法调试它们。

ExpressionMetadataProvider 给出错误“保护级别不可访问”。希望获得有关我所缺少的方面的指导,或者他们是否是在 .net core 5 中执行此操作的更好方法

【问题讨论】:

    标签: asp.net-core .net-core extension-methods asp.net-core-5.0


    【解决方案1】:

    您可以使用自定义标签助手来做到这一点:

    public class TelTagHelper : TagHelper
    {
        public string TelNum { get; set; }
        public override void Process(TagHelperContext context, TagHelperOutput output)
        {
            output.TagName = "a";
            output.Attributes.SetAttribute("href", "tel: +" + TelNum);
            output.Content.SetContent(TelNum);
        }
    }
    

    将 addTagHelper 指令添加到 Views/_ViewImports.cshtml 文件中:

    @addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers
    @addTagHelper *, YourProjectName
    

    然后在视图中,可以这样使用:

    <tel tel-num="123456789"></tel>
    

    渲染 html:

    更多详情,请参考doc

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-03-28
      • 2013-12-13
      • 2018-04-08
      • 2021-03-24
      • 2019-07-05
      • 2020-03-23
      • 1970-01-01
      相关资源
      最近更新 更多