当前位置: 移动技术网 > IT编程>开发语言>c# > C#实现文章添加内链的方法

C#实现文章添加内链的方法

2019年07月18日  | 移动技术网IT编程  | 我要评论
为什么文章要添加内链?  1.有利于读者         我喜欢内链文章的最初动机是让

为什么文章要添加内链? 

1.有利于读者

        我喜欢内链文章的最初动机是让读者在我的博客获得更好的阅读体验,并获得更多的价值。如果我的读者访问了我的一篇文章,发现不仅仅回答他需要的答案,还提供了更多相关内容的信息,让他们能在更多的相关主题去扩展阅读,他们迟早会喜欢并满意我的博客。让读者满意就是我们的目标,读者他们满意了,通常就会再次回访(这让你的博客变得有”粘性”)并和他们的朋友分享。

2.搜索引擎优化(seo)

      另外一个在你的博客相互链接文章的重要原因就是,搜索引擎在查看博客中链接的时候,既能找到内容,又能有利于建立博客索引和提高博客内容的排名。从其他外部博客建立的链接价值最高,能够提高你在搜索引擎的排名,内链虽然效果没这么好,但是也是对排名有帮助的。

3.提升页面浏览量

      在文章中增加链接能够提高读者访问其他页面内容的机会,这带来许多好处,首先它可以帮助您获得更多的访问者,如果你正在运行某种 cpm(按访问人次收费)广告,这是非常受益的。其次大量内链给你的博客浏览者创造了一个博客内容丰富的印象。我发现,当有人在你的博客上看到一个以上的页面时,他们更容易记住它,订阅它,评论它,并成为一个经常和忠实的读者。

而这三个好处看起来显得作用不明显,但是当你拥有大量内容的时候,它所能带来的好处就是无法估量的。所以建议你从博客建立的时候就开始构建内链到站内的其他文章,慢慢地这些内链积累下来的好处将会产生非常明显的好处。

 当然还有更多好处,请自行百度知,说着说着有点跑偏主题了,我们主要说用c#怎么实现此功能,话不多说,上干货!!

 第一种办法:(string src指文本字符, ilist<keyword> keys 指要匹配的关键词库集合) 

private string keyaddurl(string src, ilist<keyword> keys)
    {
      regex reg = new regex(@"(?i)(?:^|(?<!<a\b(?>[^<>]*))>)(?>[^<>]*)(?:<|$)");
      int length = 0;
      string temp = string.empty;
      return reg.replace(src, delegate (match m)
      {
        temp = m.value;
        length = temp.length;
        for (int i = 0; i < keys.count; i++)
        {
          string keywordname = regex.escape(keys[i].keyname);
          string keywordurl = regex.escape(keys[i].pcurl);
          temp = regex.replace(temp, @"(?is)^((?:(?:(?!" + keywordname + @"|</?a\b).)*<a\b(?:(?!</?a\b).)*</a>)*(?:(?!" + keywordname +
            @"|</?a\b).)*)(?<tag>" + keywordname + @")",
            @"$1<a href=""" rel="external nofollow" + keywordurl + @""" target=""_blank"" title=""${tag}"">${tag}</a>");
          if (length != temp.length)
          {
            keys.remove(keys[i]);
          }
          length = temp.length;
        }
        return temp;
      });
    }

第二种办法:(这是我自己能想到的办法)   

/// <summary>
    /// 一键匹配内链,只替换第一个匹配到的字符
    /// </summary>
    /// <param name="content">要匹配的文本字符</param>
    /// <param name="type">判断类型是否是"编辑"</param>
    /// <returns></returns>
    [httppost]
    [validateinput(false)]
    public async task<actionresult> contentreplace(string content, string type)
    {
      try
      {
        if (type == "edit")
        {
          content = webhelper.replacestrhref(content);
        }
        if (string.isnullorempty(content))
        {
          return content("请输入文本信息");
        }
        var list = await bll.getalllist();
        if (list != null)
        {
          #region 第二种办法
          // 第一次
          regex reg = null;
          int n = -1;
          dictionary<string, string> dic = new dictionary<string, string>();
          for (int i = 0; i < list.count; i++)
          {
            if (regex.match(content, list[i].keyname).success)
            {
              string pattern = $"<a href=\"{list[i].pcurl}\" target=\"_blank\">{list[i].keyname}</a>";
              reg = new regex(list[i].keyname);
              content = reg.replace(content, pattern, 1);
              if (regex.match(content, pattern).success)
              {
                //如果当前关键字链接信息成功,将当前的文本链接信息提取出来添加到字典中(dic),并以唯一标识符代替文本链接信息作为占位符。
                reg = new regex(pattern);
                n++;
                content = reg.replace(content, "{" + n + "}", 1);
                dic.add("{" + n + "}", pattern);
              }
            }
          }
          //将匹配到的文本链接信息format
          if (dic.count != 0)
          {
            int m = -1;
            foreach (string key in dic.keys)
            {
              m++;
              if (content.contains("{" + m + "}"))
              {
                content = content.replace("{" + m + "}", dic[key]);
              }
            }
          }
          #endregion
          content = webhelper.removestrimgalt(content);
          return content(content);
        }
        else
        {
          throw new exception("关键字库中没有数据,检查集合是否抛异常了");
        }
      }
      catch (exception ex)
      {
        throw ex;
      }
    }

此处也贴出上面webhelper中的两个方法:(关于作用方法都有注释)

/// <summary>
    /// 移除文本字符的a标签
    /// </summary>  
    public static string replacestrhref(string content)
    {
      var r = new regex(@"<a\s+(?:(?!</a>).)*?>|</a>", regexoptions.ignorecase);
      match m;
      for (m = r.match(content); m.success; m = m.nextmatch())
      {
        content = content.replace(m.groups[0].tostring(), "");
      }
      return content;
    }
    /// <summary>
    /// 移除字符文本img里面alt关键字包裹的内链
    /// </summary>   
    public static string removestrimgalt(string content)
    {
      regex rg2 = new regex("(?<=alt=\"<a[^<]*)</a>\"");
      if (rg2.match(content).success)
      {
        content = rg2.replace(content, "");
      }
      regex rg = new regex("(?<=alt=\")<a href=\"[^>]*>");
      if (rg.match(content).success)
      {
        content = rg.replace(content, "");
      }
      return content;
    }

总结

以上所述是小编给大家介绍的c#实现文章添加内链的方法,希望对大家有所帮助

如您对本文有疑问或者有任何想说的,请点击进行留言回复,万千网友为您解惑!

相关文章:

验证码:
移动技术网