• asp.net的cms 核心代码篇


    第一篇,我简略描述了一下我的cms标签所表示的含义。码农日记——anCMS(c#版)第一篇绑定数据

    第二篇,我将展示了标签背后真正运行的代码。asp.net的cms 原理篇

    好像开源有点多余,核心代码就下面这些。

      1 using System;
      2 using System.Collections;
      3 using System.Collections.Generic;
      4 using System.Linq;
      5 using System.Text;
      6 
      7 namespace an.helper
      8 {
      9     /// <summary>
     10     /// 查找html页面中可以转换成aspx页面的方法 的正则表达式
     11     /// </summary>
     12     public class HtmlToAspxMethod
     13     {
     14         private static T_Validate tv=new T_Validate();
     15         public static string ConvertMethodNameHtmlToAspx(string methodName)
     16         {
     17             string str="";
     18             switch(methodName)
     19             {
     20                 case "list ": str = "TextFile"; break;  //文档
     21                 case "listpager ": str = "TextFilePager"; break; //文档分页
     22                 case "alist ": str = "Article"; break;//文章列表
     23                 case "alistpager ": str = "ArticlePager"; break;//文章分页
     24                 case "plist ": str = "Products"; break;//产品列表
     25                 case "plistpager ": str = "ProductsPager"; break;//产品分页
     26                 case "olist ": str = "One"; break;//单页列表
     27                 case "olistpager ": str = "OnePager"; break;//单页分页
     28                 case "clist ": str = "Category"; break; //分类列表
     29                 case "clistpager ": str = "CategoryPager"; break; //分类列表
     30                 case "llist ": str = "Link"; break;//连接列表
     31                 case "llistpager ": str = "LinkPager"; break;//连接分页
     32                 case "ilist ": str = "Images"; break;//图片集列表
     33                 case "ilistpager ": str = "ImagesPager"; break;//图片集分页
     34                 default: break;
     35             }
     36             return str;
     37         }
     38         public static string MethodListName = "list |listpager |alist |alistpager |plist |plistpager |olist |olistpager |clist |clistpager |llist |llistpager |ilist |ilistpager ";
     39         public static string MethodFiledName = "filed|num|category|keyword|sort|page|id";
     40         /// <summary>
     41         /// 所有方法的入口,第一次运行时遍历该方法。
     42         /// </summary>
     43         public static string AllMethodContentRegex = "<!--{an:(" + MethodListName + ").*?}-->[\\s\\S]*?<!--{/an}-->";
     44         public static List<string> AllMethodContent(string content)
     45         {
     46             return Method.RegexStr(AllMethodContentRegex, content);
     47         }
     48         /// <summary>
     49         /// 获取所有的方法
     50         /// </summary>
     51         public static string AllMethodRegex = "<!--{an:(" + MethodListName + ")[^}]*}-->";
     52         public static List<string> AllMethod(string content)
     53         {
     54             return Method.RegexStr(AllMethodRegex, content);
     55         }
     56 
     57         /// <summary>
     58         /// 获取方法的函数名称
     59         /// </summary>
     60         public static string MethodNameRegex = "<!--{an:(" + MethodListName + ")";
     61         public static string MethodName(string content)
     62         {
     63             string str = "";
     64             var c=Method.RegexStr(MethodNameRegex, content);
     65             if (c.Count>0)
     66             {
     67                 str = c[0].ToString();
     68             }
     69             return str.Replace("<!--{an:", "");
     70         }
     71         /// <summary>
     72         /// 获取方法的字段和值
     73         /// </summary>        
     74         public static string MethodFiledRegex = "(" + MethodFiledName + ")=\\[[^]]+]";
     75         public static Hashtable MethodFiled(string content)
     76         {           
     77             Hashtable ht = new Hashtable();
     78             foreach (string s in Method.RegexStr(MethodFiledRegex, content))
     79             {
     80                 string[] arr = s.Split('=');
     81                 ht.Add(arr[0], arr[1].Replace("[", "").Replace("]", ""));
     82             }
     83             return ht;
     84         }
     85         public static string JsIncludeRegex = "<script.*rel=\"share\"[^>]*></script>";
     86         public static string[] JsInclude(string html)
     87         {
     88             string js = "";            
     89             List<string> list_include = Method.RegexStr(JsIncludeRegex, html);
     90             foreach (string inc in list_include)
     91             {
     92                 List<string> includeList = Method.RegexStr(@"\w+(?=\.js)", inc);
     93                 if (includeList != null)
     94                 {
     95                     js += includeList[0] + "(); ";                    
     96                     html = html.Replace(inc, "<!--#include file=\"Share/" +includeList[0]  + ".ascx\"--> ");
     97                 }
     98             }
     99             return new string[] { js, html };
    100         }
    101         /// <summary>
    102         /// 占位符
    103         /// </summary>
    104         public static string ValueOneRegex = @"\${\d+}";
    105         public static string ValueOne(string content)
    106         {      
    107             foreach (string s in Method.RegexStr(ValueOneRegex, content))
    108             {
    109                 string num = s.Replace("${", "").Replace("}", "");
    110                 content = content.Replace(s, "<%=a[" + num + "]%>");
    111             }
    112             return content;
    113         }
    114 
    115         public static string ValueOneToStringFormartRegex = @"\${\d+\|.*?}";
    116         public static string ValueOneToStringFormat(string content)
    117         {
    118             foreach (string s in Method.RegexStr(ValueOneToStringFormartRegex, content))
    119             {
    120                 string str = s.Replace("${", "").Replace("}", "");
    121                 string[] arr = str.Split('|');
    122                 string num = arr[0].ToString();
    123                 string formart = arr[1].ToString();
    124                 content = content.Replace(s, "<%=TimeFormart(a[" + num + "],\"" + formart + "\")%>");
    125             }
    126             return content;
    127         }
    128 
    129         public static string UrlRouteRegex = @"\$href\[\w+\]\[.*?\]";
    130         public static string UrlRoute(string content)
    131         {                
    132             foreach (string s in Method.RegexStr(UrlRouteRegex, content))
    133             {
    134                 //$href[index][{"aaa","bbb"},{"ccc","ddd"}]
    135                 //<%=UrlRoute("",new string[,]{{"aaa","bbb"},{"ccc","ddd"}});%>
    136                 string urlRoute = s.Replace("$href[", "<%=UrlRoute(\"").
    137                                     Replace("][", "\",new string[,]{ ").                                    
    138                                     Replace("#0", "a[0].ToString()").
    139                                     Replace("#1", "a[1].ToString()").
    140                                     Replace("#2", "a[2].ToString()").
    141                                     Replace("#3", "a[3].ToString()").
    142                                     Replace("#4", "a[4].ToString()").
    143                                     Replace("#5", "a[5].ToString()").
    144                                     Replace("#6", "a[6].ToString()").
    145                                     Replace("#7", "a[7].ToString()").
    146                                     Replace("#8", "a[8].ToString()").
    147                                     Replace("#9", "a[9].ToString()");
    148                 urlRoute = urlRoute.Substring(0,urlRoute.Length - 1);
    149                 urlRoute = urlRoute + "})%>";
    150                 content = content.Replace(s, urlRoute);
    151             }
    152             return content;
    153         }
    154         public static string PagingRegex = @"\$pager\[\w+\]\[.*?\]\[\d+\]";
    155         public static string Paging(string content)
    156         {
    157             //$pager[list][Category=$category$][15]
    158             //<%=Paging("list",new{Category=$category$}, int showCounts, object totleCounts)%>
    159             foreach (string s in Method.RegexStr(PagingRegex, content))
    160             {
    161                 int numSite=s.LastIndexOf('[');
    162                 string pager = s.Substring(0, numSite);
    163                 string num = s.Substring(numSite).Replace("[", "").Replace("]", "");
    164 
    165                 pager = pager.Replace("$pager[", "<%=Paging(\"").
    166                               Replace("][", "\",new string[,]{");
    167                 pager = pager.Substring(0, pager.Length - 1) + "}," + num + ",a[0])%>";                
    168                 content = content.Replace(s, pager);
    169             }
    170             return content;
    171         }
    172 
    173 
    174         public static string LinkHrefRegex = "(?<=<link.*href=\")[^\"]*";
    175         public static string ScriptSrcRegex = "(?<=<script.*src=\")[^\"]*";
    176         public static string ImgSrcRegex = "<img.*rel=\"share\"[^>]*/>";
    177         public static string TemplatePath = "<%=An_DoMain%>/Templates/";
    178 
    179         public static string HrefHtml(string html)
    180         {
    181             List<string> list_link = Method.RegexStr(LinkHrefRegex, html);
    182             foreach (string link in list_link.Distinct())
    183             {
    184                 html = html.Replace(link, TemplatePath + link);
    185             }
    186             List<string> list_script = Method.RegexStr(ScriptSrcRegex, html);
    187             foreach (string script in list_script.Distinct())
    188             {
    189                 html = html.Replace(script, TemplatePath + script);
    190             }
    191             List<string> list_img = Method.RegexStr(ImgSrcRegex, html);
    192             foreach (string img in list_img.Distinct())
    193             {
    194                 html = html.Replace(img, img.Replace("src=\"", "src=\"" + TemplatePath));
    195             }
    196             return html;
    197         }
    198 
    199 
    200         //public static string ForRegex = "<!--{for}-->[\\s\\S]*?<!--{/for}-->";
    201         //public static string For(string arr, string content)
    202         //{
    203         //    foreach (string s in Method.RegexStr(ForRegex, content))
    204         //    {                
    205         //        string str = "";
    206         //        str = "<%if(1==1){var arr=" + arr + ";foreach(var s in arr){%>";
    207         //        str = str + s.Replace("<!--{for}-->", "").Replace("<!--{/for}-->", "") + "<%}}%>";
    208         //        content=content.Replace(s,str);
    209         //    }
    210         //    return content;
    211         //}
    212 
    213         public static string FnSplitContentRegex = @"\$split\[.*?\]\[.*?\]";
    214         public static string FnSplitRegex = @"<!--{split}-->[\s\S]*?<!--{/split}-->";
    215         public static string FnSplit(string content)
    216         {
    217             foreach (string s in Method.RegexStr(FnSplitRegex, content))
    218             {
    219                 var list_split = Method.RegexStr(FnSplitContentRegex, content);
    220                 string splitMethod = "";
    221                 if (list_split != null)
    222                 {
    223                     splitMethod = list_split[0].ToString();
    224                 }
    225                 if (splitMethod != "")
    226                 {
    227                     string source = "", separator = "";
    228                     var list_split_value = splitMethod.Split(']');
    229                     if (list_split_value.Length > 0)
    230                     {
    231                         source = list_split_value[0].Replace("$split[", "");
    232                         separator = list_split_value[1].Replace("[", "");
    233                         if (source != "")
    234                         {
    235                             string str = "";
    236                             if (tv.IsInteger(source) == "")
    237                             {
    238                                 str = "<%if(1==1){var arr=a[" + source + "];foreach(var s in arr.Split('"+separator+"')){%>";
    239                             }
    240                             else
    241                             {
    242                                 str = "<%if(1==1){var arr=\"" + source + "\";foreach(var s in arr.Split('" + separator + "')){%>";
    243                             }
    244                             str += s.Replace(splitMethod, "<%=s%>").Replace("<!--{split}-->", "").Replace("<!--{/split}-->", "");
    245                             str += "<%}}%>";
    246                             content = content.Replace(s, str);
    247                         }
    248                     }
    249 
    250                 }    
    251             }
    252             return content;
    253         }
    254 
    255 
    256 
    257 
    258 
    259         /// <summary>
    ///www.ancms.net 260 /// 将html方法转换成aspx的方法 261 /// </summary> 262 /// <param name="methodName"></param> 263 /// <param name="content"></param> 264 /// <returns></returns> 265 public static AspxFiled HtmlFiledToAspxFiled(string methodName, string content) 266 { 267 AspxFiled af = new AspxFiled(); 268 Hashtable ht = MethodFiled(content); 269 foreach (DictionaryEntry h in ht) 270 { 271 if (h.Key.ToString() == "filed") 272 { 273 af.Filed = h.Value.ToString(); 274 } 275 if (h.Key.ToString() == "num") 276 { 277 if (tv.IsInteger(h.Value.ToString()) == "") 278 { 279 af.ShowCounts = Convert.ToInt32(h.Value.ToString()); 280 if (af.ShowCounts < 0) 281 { 282 af.ShowCounts = 1; 283 } 284 } 285 else 286 { 287 af.ShowCounts = 10; 288 } 289 } 290 if (h.Key.ToString() == "sort") 291 { 292 af.Sort = h.Value.ToString(); 293 } 294 if (h.Key.ToString() == "page") 295 { 296 if (h.Value.ToString() == "true") 297 { 298 af.Current = "An_Current"; 299 } 300 else 301 { 302 af.Current = "1"; 303 } 304 } 305 if (h.Key.ToString() == "category") 306 { 307 af.CategoryID = h.Value.ToString(); 308 } 309 if (h.Key.ToString() == "keyword") 310 { 311 af.Keyword = h.Value.ToString(); 312 } 313 if (h.Key.ToString() == "id") 314 { 315 af.ID = h.Value.ToString(); 316 } 317 } 318 return af; 319 } 320 321 public static string AspxMethodDataSet(string methodName,string content,string datatableName) 322 { 323 AspxFiled af = HtmlFiledToAspxFiled(methodName, content); 324 string str = ""; 325 if (methodName.Contains("pager")) 326 { 327 if (methodName == "clistpager ") 328 { 329 str = "MyHashtable.Add(\"" + datatableName + "\", " + ConvertMethodNameHtmlToAspx(methodName) + "(\"" + af.CategoryID + "\"));"; 330 } 331 else 332 { 333 str = "MyHashtable.Add(\"" + datatableName + "\", " + ConvertMethodNameHtmlToAspx(methodName) + "(\"" + af.ID + "\",\"" + af.CategoryID + "\",\"" + af.Keyword + "\"));"; 334 } 335 } 336 else 337 { 338 if (methodName == "clist ") 339 { 340 str = "MyHashtable.Add(\"" + datatableName + "\", " + ConvertMethodNameHtmlToAspx(methodName) + "(\"" + af.Filed + "\", " + af.Current + ", " + af.ShowCounts + ", \"" + af.Sort + "\", \"" + af.CategoryID + "\"));"; 341 } 342 else 343 { 344 str = "MyHashtable.Add(\"" + datatableName + "\", " + ConvertMethodNameHtmlToAspx(methodName) + "(\"" + af.Filed + "\", " + af.Current + ", " + af.ShowCounts + ", \"" + af.Sort + "\", \"" + af.ID + "\", \"" + af.CategoryID + "\", \"" + af.Keyword + "\"));"; 345 } 346 } 347 return str; 348 } 349 public static string AspxMethodDataRow(string datatableName,string content) 350 { 351 return "<%if(1==1){var myRows = MyRows(\"" + datatableName + "\"); if (myRows != null){foreach (var a in myRows){ %>" + content + "<%}}else{%><p style='margin:10px;'>没有相关信息</p><%}}%>"; 352 } 353 354 public static string ServerFunction(string content) 355 { 356 return "<script runat=\"server\">protected override void OnLoad(EventArgs e){" + content + "}</script>"; 357 } 358 public static string ServerFunction(string content, string fnName) 359 { 360 return "<script runat=\"server\">public void " + fnName + "(){" + content + "}</script>"; 361 } 362 363 } 364 365 public class AspxFiled 366 { 367 private string _Filed; 368 public string Filed 369 { 370 get{ return _Filed; } 371 set 372 { 373 if (value.Contains("TextFile.ID,")) 374 { 375 _Filed = value; 376 } 377 else 378 { 379 _Filed = "TextFile.ID," + value; 380 } 381 } 382 } 383 private string _ID = ""; 384 public string ID 385 { 386 get { return _ID; } 387 set { _ID = value; } 388 } 389 private string _CategoryID = ""; 390 public string CategoryID 391 { 392 get { return _CategoryID; } 393 set { _CategoryID = value; } 394 } 395 private string _Keyword = ""; 396 public string Keyword 397 { 398 get { return _Keyword; } 399 set { _Keyword = value; } 400 } 401 402 private string _Sort; 403 public string Sort 404 { 405 get { return _Sort; } 406 set { _Sort = value; } 407 } 408 private int _ShowCounts = 0; 409 public int ShowCounts 410 { 411 get{return _ShowCounts;} 412 set { _ShowCounts = value; } 413 } 414 private string _Current = "1"; 415 public string Current 416 { 417 get { return _Current; } 418 set { _Current = value;} 419 } 420 } 421 /// <summary> 422 /// html和aspx的方法名称对应替换
    /// www.ancms.net
    423 /// </summary> 424 public class ConvertHtmlToAspx 425 { 426 public string DataTableName(int num,string name) 427 { 428 return Method.Md5(DateTime.Now.ToLongDateString() + new Random().Next(1000) + num + new Random().Next(1000) + name); 429 } 430 public string ToAspx(string html,string ascxName) 431 { 432 html = AllToAspx(html); 433 List<string> list_AllMethodContent = HtmlToAspxMethod.AllMethodContent(html); 434 string load = ""; 435 int i = 0; 436 foreach (string allMethodContent in list_AllMethodContent) 437 { 438 i++; 439 string allMethod = HtmlToAspxMethod.AllMethod(allMethodContent)[0];//<!--{an:list}--> 440 string methodName = HtmlToAspxMethod.MethodName(allMethod);//方法名称<!--{an:list 441 Hashtable methodFiled = HtmlToAspxMethod.MethodFiled(allMethod);//filed=[title] sort=[time desc] 442 string content = allMethodContent.Replace(allMethod, "").Replace("<!--{/an}-->", ""); 443 content = HtmlToAspxMethod.ValueOne(content); 444 content = HtmlToAspxMethod.ValueOneToStringFormat(content); 445 string dataTableName = DataTableName(i,ascxName); 446 load += HtmlToAspxMethod.AspxMethodDataSet(methodName, allMethod, dataTableName); 447 content = HtmlToAspxMethod.AspxMethodDataRow(dataTableName, content); 448 html = html.Replace(allMethodContent, content); 449 } 450 if (ascxName == "") 451 { 452 string[] js=HtmlToAspxMethod.JsInclude(html); 453 html = js[1]; 454 load += js[0]; 455 load = HtmlToAspxMethod.ServerFunction(load); 456 } 457 else 458 { 459 load = HtmlToAspxMethod.ServerFunction(load, ascxName); 460 } 461 html = HtmlToAspxMethod.HrefHtml(html);//链接转换 462 return load + html; 463 } 464 private string AllToAspx(string html) 465 { 466 html = html.Replace("$categoryname", "<%=An_CategoryName%>");
    467 //html = html.Replace("$title$", "<%=An_Title%>").//页面标题 468 // Replace("$keywords$", "<%=An_KeyWords%>").//页面关键词 469 // Replace("$description$", "<%=An_Description%>").//页面描述 470 // Replace("$domain$", "<%=An_DoMain%>").//网站域名 471 // Replace("$categoryname$", "<%=An_CategoryName%>").//分类名称 472 // Replace("$contact$", "\"+An_Contact+\"").//联系我们 473 // Replace("$id$", "An_ID").//明细ID 474 // Replace("$category$", "An_CategoryID").//分类ID 475 // Replace("$tongji$", "<%=An_TongJi%>"). 476 // Replace("$keyword$", "\"+An_KeyWord+\"");//搜索的关键词 477 html = HtmlToAspxMethod.UrlRoute(html); 478 html = HtmlToAspxMethod.Paging(html); 479 html = HtmlToAspxMethod.FnSplit(html); 480 481 return html; 482 } 483 484 } 485 }

    呵呵,会不会太肤浅了。

    新创建了一群:37703194  希望大家可以共同讨论。

    主要就是通过上面这个代码,将html模版通过替换后变成aspx来运行。

    我要去完善我的CMS啦。

  • 相关阅读:
    人机猜拳
    M1分数分配
    postmortem report of period M1
    视频文档分享网站场景测试说明书
    功能规格说明书
    11.9Daily Scrum
    11.6Daily Scrum
    需求博客
    11.5Daily Scrum
    11.3Daily Scrum
  • 原文地址:https://www.cnblogs.com/ancms/p/2820558.html
Copyright © 2020-2023  润新知