码迷,mamicode.com
首页 > Web开发 > 详细

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

时间:2018-04-22 21:55:07      阅读:322      评论:0      收藏:0      [点我收藏+]

标签:help   with   显示   data   微软官方   文档   view   title   分享   

 

 微软官方文档地址

  基本步骤:

    技术分享图片

    然后添加到

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

技术分享图片

技术分享图片

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

小例子:

  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 中的-----标记帮助程序

标签:help   with   显示   data   微软官方   文档   view   title   分享   

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!