首页 > Web开发 > 详细

.net core 中的-----标记帮助程序

时间:2018-04-22 21:20:59      阅读:358      评论:0      收藏:0      [点我收藏+]

 

 微软官方文档地址

  基本步骤:

    技术分享图片

    然后添加到

技术分享图片技术分享图片

技术分享图片

技术分享图片

  具体编写规则请参考最上面的地址

小例子:

  1.绑定参数

技术分享图片

技术分享图片

技术分享图片

技术分享图片

技术分享图片

 

  2.根据参数选择是否显示html内容

技术分享图片

技术分享图片

 

技术分享图片

技术分享图片

技术分享图片

   3.避免标记帮助程序冲突

[HtmlTargetElement("p")]
public class AutoLinkerHttpTagHelper : TagHelper
{
    public override async Task ProcessAsync(TagHelperContext context, TagHelperOutput output)
    {
        var childContent = await output.GetChildContentAsync();
        // Find Urls in the content and replace them with their anchor tag equivalent.
        output.Content.SetHtmlContent(Regex.Replace(
             childContent.GetContent(),
             @"\b(?:https?://)(\S+)\b",
              "<a target=\"_blank\" href=\"$0\">$0</a>"));  // http link version}
    }
}

技术分享图片

技术分享图片

  在加一个

 

    [HtmlTargetElement("p")]
    public class AutoLinkerWwwTagHelper : TagHelper
    {
        public override async Task ProcessAsync(TagHelperContext context, TagHelperOutput output)
        {
            var childContent = await output.GetChildContentAsync();
            // Find Urls in the content and replace them with their anchor tag equivalent.
            output.Content.SetHtmlContent(Regex.Replace(
                childContent.GetContent(),
                 @"\b(www\.)(\S+)\b",
                 "<a target=\"_blank\" href=\"http://$0\">$0</a>"));  // www version
        }
    }

技术分享图片

可以将代码变成这样

技术分享图片

技术分享图片

 

.net core 中的-----标记帮助程序

原文:https://www.cnblogs.com/student-note/p/8909096.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!