修改BlogEngine.NET:给摘要显示添加图片显示的另一种方法

方法如下:

打开theme目录下面的PostView.ascx文件。

定位到<asp:PlaceHolder ID="BodyContent" runat="server" />

在之前添加一点代码<%= getImage(base.ShowExcerpt,Post.Content) %>

修改完即成如下:

<div class="entry"><%= getImage(base.ShowExcerpt,Post.Content) %><asp:PlaceHolder ID="BodyContent" runat="server" /></div>

当然别忘了getImage()函数的代码要加进去,PostView.ascx文件的最后添加一段:

 <script language="CS" runat="server">
 
      string getImage(bool ShowExcerpt, string input)
      {
          if (!ShowExcerpt || input == null)
              return "";
 
            string pain = input;//字符串
            string pattern = @"<img(.|\n)+?>";
 
            System.Text.RegularExpressions.Match m = System.Text.RegularExpressions.Regex.Match(input, pattern, 
                System.Text.RegularExpressions.RegexOptions.IgnoreCase | System.Text.RegularExpressions.RegexOptions.Multiline);
            if (m.Success)
            {
                string src = getSrc (m.Value);
                string img = string.Format("<img align=\"right\" style=\"border-bottom: 0px; border-left: 0px; display: inline; margin-left: 5px; border-top: 0px; margin-right: 5px; border-right: 0px\" border=\"0\" width=\"100\" {0}  />",src);
                return img;
            }
            else
            {
                return "";
            }
            
        }
 
        string getSrc(string input)
        {           
            string pattern = "src=[\'|\"](.+?)[\'|\"]";
 
            System.Text.RegularExpressions.Regex reImg = new System.Text.RegularExpressions.Regex(pattern, 
                System.Text.RegularExpressions.RegexOptions.IgnoreCase | System.Text.RegularExpressions.RegexOptions.Multiline);
            System.Text.RegularExpressions.Match mImg = reImg.Match(input);
            if (mImg.Success)
            {
                return mImg.Value;
            }
            return "";
         
        }
   </script>
原文地址:https://www.cnblogs.com/MaxWoods/p/1799523.html