ASP.net's CMS core code article _ Practical Tips

Source: Internet
Author: User
In the first article, I briefly describe what my CMS label means. Ancms (C # Edition) first bound data

Second, I'll show you the code that actually runs behind the tag. ASP.net's CMS principle article

As if open source is a bit redundant, the core code is the following.
Copy Code code as follows:

Using System;
Using System.Collections;
Using System.Collections.Generic;
Using System.Linq;
Using System.Text;

Namespace An.helper
{
<summary>
Find regular expressions for methods that can be converted to ASPX pages in an HTML page
</summary>
public class Htmltoaspxmethod
{
private static T_validate tv=new t_validate ();
public static string Convertmethodnamehtmltoaspx (String methodname)
{
String Str= "";
Switch (methodname)
{
Case "List": str = "Textfile"; Break Document
Case "Listpager": str = "Textfilepager"; Break Document Paging
Case "Alist": str = "Article"; break;//articles List
Case "Alistpager": str = "Articlepager"; break;//article pagination
Case "plist": str = "Products"; break;//Product List
Case "Plistpager": str = "Productspager"; break;//Product Paging
Case "Olist": str = "one"; break;//single Page List
Case "Olistpager": str = "Onepager"; break;//single page Paging
Case "CList": str = "Category"; Break Category List
Case "Clistpager": str = "Categorypager"; Break Category List
Case "Llist": str = "Link"; break;//Connection List
Case "Llistpager": str = "Linkpager"; break;//Connection Paging
Case "IList": str = "Images"; break;//List of picture sets
Case "Ilistpager": str = "Imagespager"; break;//Picture Set Paging
Default:break;
}
return str;
}
public static string methodlistname = "List |listpager |alist |alistpager |plist |plistpager |olist |olistpager |clist |cl Istpager |llist |llistpager |ilist |ilistpager ";
public static string methodfiledname = "Filed|num|category|keyword|sort|page|id";
<summary>
The entry for all methods that traverse the method the first time it is run.
</summary>
public static string Allmethodcontentregex = "<!--{a: (" + Methodlistname + "). *?} -->[\\s\\s]*?<!--{/an}--> ";
public static list<string> Allmethodcontent (string content)
{
Return Method.regexstr (Allmethodcontentregex, content);
}
<summary>
Get all the methods
</summary>
public static string Allmethodregex = "<!--{a: (" + Methodlistname + ") [^}]*}-->";
public static list<string> Allmethod (string content)
{
Return Method.regexstr (Allmethodregex, content);
}

<summary>
Gets the function name of the method
</summary>
public static string Methodnameregex = "<!--{a: (" + Methodlistname + ")";
public static string methodname (String content)
{
String str = "";
var c=method.regexstr (Methodnameregex, content);
if (c.count>0)
{
str = c[0]. ToString ();
}
Return str. Replace ("<!--{an:", "");
}
<summary>
Get the field and value of a method
</summary>
public static string Methodfiledregex = "(" + Methodfiledname + ") =\\[[^]]+]";
public static Hashtable methodfiled (string content)
{
Hashtable ht = new Hashtable ();
foreach (String s in Method.regexstr (Methodfiledregex, content))
{
string[] arr = s.split (' = ');
Ht. ADD (Arr[0], arr[1]. Replace ("[", ""). Replace ("]", ""));
}
return HT;
}
public static string Jsincluderegex = "<script.*rel=\" share\ "[^>]*></script>";
public static string[] Jsinclude (string html)
{
String js = "";
list<string> list_include = METHOD.REGEXSTR (Jsincluderegex, HTML);
foreach (String inc in List_include)
{
list<string> includelist = method.regexstr (@ "\w+ (? =\.js)", Inc.);
if (includelist!= null)
{
JS + + includelist[0] + "();";
html = HTML. Replace (inc, "<!--#include file=\" share/"+includelist[0] +". Ascx\ "-->");
}
}
return new string[] {js, html};
}
<summary>
Placeholder
</summary>
public static string Valueoneregex = @ "\${\d+}";
public static string Valueone (string content)
{
foreach (String s in Method.regexstr (Valueoneregex, content))
{
String num = S.replace ("${", ""). Replace ("}", "");
Content = content. Replace (S, "<%=a[" + num + "]%>");
}
return content;
}

public static string Valueonetostringformartregex = @ "\${\d+\|. *?}";
public static string Valueonetostringformat (string content)
{
foreach (String s in Method.regexstr (Valueonetostringformartregex, content))
{
String str = s.replace ("${", ""). Replace ("}", "");
string[] arr = str. Split (' | ');
String num = Arr[0]. ToString ();
String formart = Arr[1]. ToString ();
Content = content. Replace (S, "<%=timeformart (a[" + num + "],\" "+ Formart +" \ ")%>");
}
return content;
}

public static string Urlrouteregex = @ "\ $href \[\w+\]\[.*?\]";
public static string Urlroute (string content)
{
foreach (String s in Method.regexstr (Urlrouteregex, content))
{
$href [index][{"AAA", "BBB"},{"CCC", "DDD"}]
<%=urlroute ("", New string[,]{{"AAA", "BBB"},{"CCC", "DDD"}});%>
String urlroute = S.replace ("$href [", "<%=urlroute (\").
Replace ("[", "\", new string[,]{).
Replace ("#0", "a[0]." ToString () ").
Replace ("#1", "a[1]." ToString () ").
Replace ("#2", "a[2]." ToString () ").
Replace ("#3", "a[3]." ToString () ").
Replace ("#4", "a[4]." ToString () ").
Replace ("#5", "a[5]." ToString () ").
Replace ("#6", "a[6]." ToString () ").
Replace ("#7", "a[7]." ToString () ").
Replace ("#8", "a[8]." ToString () ").
Replace ("#9", "a[9]." ToString ());
Urlroute = urlroute.substring (0,urlroute.length-1);
Urlroute = Urlroute + "})%>";
Content = content. Replace (S, Urlroute);
}
return content;
}
public static string Pagingregex = @ "\ $pager \[\w+\]\[.*?\]\[\d+\]";
public static string paging (string content)
{
$pager [list][category= $category $][15]
<%=paging ("list", new{category= $category $}, int showcounts, object totlecounts)%>
foreach (String s in Method.regexstr (Pagingregex, content))
{
int Numsite=s.lastindexof (' [');
String pager = s.substring (0, Numsite);
String num = s.substring (numsite). Replace ("[", ""). Replace ("]", "");

Pager = Pager. Replace ("$pager [", "<%=paging (\").
Replace ("[", "\", new string[,]{);
Pager = Pager. Substring (0, Pager. Length-1) + "}," + num + ", a[0])%>";
Content = content. Replace (S, pager);
}
return content;
}


public static string Linkhrefregex = "(? <=<link.*href=\") [^\ "]*";
public static string Scriptsrcregex = "(? <=<script.*src=\") [^\ "]*";
public static string Imgsrcregex = "]*/>";
public static string TemplatePath = "<%=an_domain%>/templates/";

public static string hrefhtml (string html)
{
list<string> List_link = METHOD.REGEXSTR (Linkhrefregex, HTML);
foreach (string link in list_link. Distinct ())
{
html = HTML. Replace (link, templatepath + link);
}
list<string> list_script = METHOD.REGEXSTR (Scriptsrcregex, HTML);
foreach (String script in List_script. Distinct ())
{
html = HTML. Replace (script, TemplatePath + script);
}
list<string> list_img = METHOD.REGEXSTR (Imgsrcregex, HTML);
foreach (String img in list_img. Distinct ())
{
html = HTML. Replace (IMG, IMG. Replace ("src=\" "," src=\ "" + TemplatePath));
}
return HTML;
}


public static string Forregex = "<!--{for}-->[\\s\\s]*?<!--{/for}-->";
public static string for (string arr, string content)
//{
foreach (String s in Method.regexstr (Forregex, content))
// {
String str = "";
str = "<%if (1==1) {var arr=" + arr + "; foreach (var s in arr) {%>";
str = str + s.replace ("<!--{for}-->", ""). Replace ("<!--{/for}-->", "") + "<%}}%>";
Content=content. Replace (S,STR);
// }
return content;
//}

public static string Fnsplitcontentregex = @ "\ $split \[.*?\]\[.*?\]";
public static string Fnsplitregex = @ "<!--{split}-->[\s\s]*?<!--{/split}-->";
public static string Fnsplit (string content)
{
foreach (String s in Method.regexstr (Fnsplitregex, content))
{
var list_split = method.regexstr (Fnsplitcontentregex, content);
String splitmethod = "";
if (list_split!= null)
{
Splitmethod = list_split[0]. ToString ();
}
if (Splitmethod!= "")
{
String Source = "", separator = "";
var list_split_value = Splitmethod.split ('] ');
if (List_split_value. Length > 0)
{
Source = List_split_value[0]. Replace ("$split [", "");
Separator = list_split_value[1]. Replace ("[", "");
if (source!= "")
{
String str = "";
if (TV. Isinteger (source) = = "")
{
str = "<%if (1==1) {var arr=a[" + source + "];foreach" (Var s in arr.) Split (' "+separator+")) {%> ";
}
Else
{
str = "<%if (1==1) {var arr=\" "+ source +" \ "; foreach (var s in arr.) Split (' "+ separator +")) {%> ";
}
str = s.replace (Splitmethod, "<%=s%>"). Replace ("<!--{split}-->", ""). Replace ("<!--{/split}-->", "");
STR + + "<%}}%>";
Content = content. Replace (S, str);
}
}

}
}
return content;
}





<summary>
Ways to convert HTML methods into ASPX
</summary>
<param name= "methodname" ></param>
<param name= "Content" ></param>
<returns></returns>
public static aspxfiled htmlfiledtoaspxfiled (String methodname, string content)
{
Aspxfiled af = new aspxfiled ();
Hashtable ht = methodfiled (content);
foreach (DictionaryEntry h in HT)
{
if (h.key.tostring () = = "Filed")
{
Af. Filed = H.value.tostring ();
}
if (h.key.tostring () = = "num")
{
if (TV. Isinteger (h.value.tostring ()) = = "")
{
Af. showcounts = Convert.ToInt32 (h.value.tostring ());
if (AF. Showcounts < 0)
{
Af. showcounts = 1;
}
}
Else
{
Af. Showcounts = 10;
}
}
if (h.key.tostring () = = "Sort")
{
Af. Sort = H.value.tostring ();
}
if (h.key.tostring () = = "Page")
{
if (h.value.tostring () = = "true")
{
Af. Current = "An_current";
}
Else
{
Af. Current = "1";
}
}
if (h.key.tostring () = = "category")
{
Af. CategoryID = H.value.tostring ();
}
if (h.key.tostring () = = "Keyword")
{
Af. Keyword = H.value.tostring ();
}
if (h.key.tostring () = = "id")
{
Af.id = H.value.tostring ();
}
}
Return AF;
}

public static string Aspxmethoddataset (String methodname,string content,string datatablename)
{
Aspxfiled af = htmlfiledtoaspxfiled (methodname, content);
String str = "";
if (Methodname.contains ("pager"))
{
if (methodname = = "Clistpager")
{
str = "Myhashtable.add (\" "+ dataTableName +" \ "," + convertmethodnamehtmltoaspx (methodname) + "(\" + AF.) CategoryID + "));";
}
Else
{
str = "Myhashtable.add (\" "+ dataTableName +" \ "," + convertmethodnamehtmltoaspx (methodname) + "(\" "+ af.id +" \ ", \" "+ Af. CategoryID + "\", \ "" + AF. Keyword + "));";
}
}
Else
{
if (methodname = = "CList")
{
str = "Myhashtable.add (\" "+ dataTableName +" \ "," + convertmethodnamehtmltoaspx (methodname) + "(\" + AF.) Filed + "\", "+ AF." Current + "," + AF. Showcounts + ", \" "+ AF. Sort + "\", \ "" + AF. CategoryID + "));";
}
Else
{
str = "Myhashtable.add (\" "+ dataTableName +" \ "," + convertmethodnamehtmltoaspx (methodname) + "(\" + AF.) Filed + "\", "+ AF." Current + "," + AF. Showcounts + ", \" "+ AF. Sort + "\", \ "" + af.id + "\", \ "+ AF. CategoryID + "\", \ "" + AF. Keyword + "));";
}
}
return str;
}
public static string Aspxmethoddatarow (string datatablename,string content)
{
Return ' <%if (1==1) {var myrows = Myrows (\ "+ datatablename +" \ "); if (myrows!= null) {foreach (var a in myrows) {%> "+ content +" <%}}else{%><p style= ' margin:10px; > No relevant information </p><%}}%> ";
}

public static string Serverfunction (string content)
{
Return "<script runat=\" Server\ ">protected override void OnLoad (EventArgs e) {" + content + "}</script>";
}
public static string Serverfunction (string content, String fnname)
{
Return "<script runat=\" Server\ ">public void" + FnName + "() {" + content + "}</script>";
}

}

public class aspxfiled
{
private string _filed;
public string Filed
{
get{return _filed;}
Set
{
if (value. Contains ("Textfile.id,"))
{
_filed = value;
}
Else
{
_filed = "Textfile.id," + value;
}
}
}
private string _id = "";
public string ID
{
get {return _id;}
set {_id = value;}
}
private string _categoryid = "";
public string CategoryID
{
get {return _categoryid;}
set {_categoryid = value;}
}
private string _keyword = "";
public string Keyword
{
get {return _keyword;}
set {_keyword = value;}
}

private string _sort;
public string Sort
{
get {return _sort;}
set {_sort = value;}
}
private int _showcounts = 0;
public int Showcounts
{
Get{return _showcounts;}
set {_showcounts = value;}
}
private String _current = "1";
public string Current
{
get {return _current;}
set {_current = value;}
}
}
<summary>
HTML and ASPX method names correspond to replacements
</summary>
public class Converthtmltoaspx
{
public string datatablename (int num,string name)
{
Return Method.md5 (DateTime.Now.ToLongDateString () + new Random (). Next (1000) + num + new Random (). Next (1000) + name);
}
public string toaspx (String html,string ascxname)
{
HTML = alltoaspx (HTML);
list<string> list_allmethodcontent = htmltoaspxmethod.allmethodcontent (HTML);
String load = "";
int i = 0;
foreach (String allmethodcontent in List_allmethodcontent)
{
i++;
String allmethod = Htmltoaspxmethod.allmethod (allmethodcontent) [0];//<!--{an:list}-->
String methodname = Htmltoaspxmethod.methodname (Allmethod);//Method name <!--{An:list
Hashtable methodfiled = htmltoaspxmethod.methodfiled (Allmethod);//filed=[title] Sort=[time desc]
String content = Allmethodcontent.replace (Allmethod, ""). Replace ("<!--{/an}-->", "");
Content = Htmltoaspxmethod.valueone (content);
Content = Htmltoaspxmethod.valueonetostringformat (content);
String datatablename = dataTableName (i,ascxname);
Load + = Htmltoaspxmethod.aspxmethoddataset (methodname, Allmethod, datatablename);
Content = Htmltoaspxmethod.aspxmethoddatarow (datatablename, content);
html = HTML. Replace (allmethodcontent, content);
}
if (Ascxname = "")
{
String[] Js=htmltoaspxmethod.jsinclude (HTML);
html = js[1];
Load + + js[0];
Load = htmltoaspxmethod.serverfunction (load);
}
Else
{
Load = htmltoaspxmethod.serverfunction (load, ascxname);
}
HTML = htmltoaspxmethod.hrefhtml (HTML);//Link conversion
return load + HTML;
}
private string alltoaspx (string html)
{
html = HTML. Replace ("$categoryname", "<%=An_CategoryName%>");
html = HTML. Replace ("$title $", "<%=An_Title%>").//Page title
Replace ("$keywords $", "<%=An_KeyWords%>").//Page Keywords
Replace ("$description $", "<%=An_Description%>").//Page description
Replace ("$domain $", "<%=An_DoMain%>").//Site domain name
Replace ("$categoryname $", "<%=An_CategoryName%>").//Category Name
Replace ("$contact $", "\" +an_contact+\ "")./Contact Us
Replace ("$id $", "an_id").//detail ID
Replace ("$category $", "An_categoryid").//Category ID
Replace ("$tongji $", "<%=An_TongJi%>").
Replace ("$keyword $", "\" +an_keyword+\ ");//Search keywords
HTML = htmltoaspxmethod.urlroute (HTML);
HTML = htmltoaspxmethod.paging (HTML);
HTML = htmltoaspxmethod.fnsplit (HTML);

return HTML;
}

}
}

Oh, it will be too superficial.

The main thing is to use the code above to run the HTML template by replacing it into ASPX.

I'm going to perfect my cms.
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.