第一篇,我简略描述了一下我的cms标签所表示的含义。anCMS(c#版)第一篇绑定数据
第二篇,我将展示了标签背后真正运行的代码。asp.net的cms 原理篇
好像开源有点多余,核心代码就下面这些。
复制代码 代码如下:
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace an.helper
{
/// <summary>
/// 查找html页面中可以转换成aspx页面的方法 的正则表达式
/// </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; //文档
case "listpager ": str = "TextFilePager"; break; //文档分页
case "alist ": str = "Article"; break;//文章列表
case "alistpager ": str = "ArticlePager"; break;//文章分页
case "plist ": str = "Products"; break;//产品列表
case "plistpager ": str = "ProductsPager"; break;//产品分页
case "olist ": str = "One"; break;//单页列表
case "olistpager ": str = "OnePager"; break;//单页分页
case "clist ": str = "Category"; break; //分类列表
case "clistpager ": str = "CategoryPager"; break; //分类列表
case "llist ": str = "Link"; break;//连接列表
case "llistpager ": str = "LinkPager"; break;//连接分页
case "ilist ": str = "Images"; break;//图片集列表
case "ilistpager ": str = "ImagesPager"; break;//图片集分页
default: break;
}
return str;
}
public static string MethodListName = "list |listpager |alist |alistpager |plist |plistpager |olist |olistpager |clist |clistpager |llist |llistpager |ilist |ilistpager ";
public static string MethodFiledName = "filed|num|category|keyword|sort|page|id";
/// <summary>
/// 所有方法的入口,第一次运行时遍历该方法。
/// </summary>
public static string AllMethodContentRegex = "<!--{an:(" + MethodListName + ").*?}-->[\\s\\S]*?<!--{/an}-->";
public static List<string> AllMethodContent(string content)
{
return Method.RegexStr(AllMethodContentRegex, content);
}
/// <summary>
/// 获取所有的方法
/// </summary>
public static string AllMethodRegex = "<!--{an:(" + MethodListName + ")[^}]*}-->";
public static List<string> AllMethod(string content)
{
return Method.RegexStr(AllMethodRegex, content);
}
/// <summary>
/// 获取方法的函数名称
/// </summary>
public static string MethodNameRegex = "<!--{an:(" + 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>
/// 获取方法的字段和值
/// </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>
/// 占位符
/// </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 = "<img.*rel=\"share\"[^>]*/>";
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>
/// 将html方法转换成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;'>没有相关信息</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和aspx的方法名称对应替换
/// </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);//方法名称<!--{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);//链接转换
return load + html;
}
private string AllToAspx(string html)
{
html = html.Replace("$categoryname", "<%=An_CategoryName%>");
//html = html.Replace("$title$", "<%=An_Title%>").//页面标题
// Replace("$keywords$", "<%=An_KeyWords%>").//页面关键词
// Replace("$description$", "<%=An_Description%>").//页面描述
// Replace("$domain$", "<%=An_DoMain%>").//网站域名
// Replace("$categoryname$", "<%=An_CategoryName%>").//分类名称
// Replace("$contact$", "\"+An_Contact+\"").//联系我们
// Replace("$id$", "An_ID").//明细ID
// Replace("$category$", "An_CategoryID").//分类ID
// Replace("$tongji$", "<%=An_TongJi%>").
// Replace("$keyword$", "\"+An_KeyWord+\"");//搜索的关键词
html = HtmlToAspxMethod.UrlRoute(html);
html = HtmlToAspxMethod.Paging(html);
html = HtmlToAspxMethod.FnSplit(html);
return html;
}
}
}
呵呵,会不会太肤浅了。
主要就是通过上面这个代码,将html模版通过替换后变成aspx来运行。
我要去完善我的CMS啦。
相关推荐:
ChatGPT怎么突然不能打开了?你需要了解的原因与解决办法,ai写作有什么问题吗怎么解决
《收录情况:数字时代的网络信息检索与价值体现》,山东全域营销推广软件客服电话
专业关键词助力SEO优化,让你的内容脱颖而出,东营响应式网站优化
seo网赚什么意思,网站seo赚钱 ,ai打不开ai
seo自己做什么,自己做seo需要花钱吗 ,ai850775
SEO运维:提升网站排名的核心战略,邢台网站建设优化建站
亚马逊的seo是什么阿,亚马逊seo项目 ,中考用ai写作会判0分吗
SEO客服:如何提升客户体验与业务转化的双赢策略,鄂州网站建设公司教程
SEO优化如何进行:提升网站排名,轻松超越竞争对手,ai写作怎么操作手机
SEO管家:为您的网站保驾护航的智能SEO助手,网站推广作用有哪些类型
SEO要素:优化网站排名的关键因素全解析,棒球大联盟营销推广文案
“多网建站”助力企业跨越发展,开启全新数字化时代,海南网站优化电池
智能AI写作生成:如何借助人工智能提升创作效率与质量
seo软件叫什么,seo软件视频教程 ,eps ai 缩略图
SEO热词:提升网站排名的关键秘诀,一句话营销推广怎么写好
ChatGPT为什么用不了了?背后的真相揭秘!,ai写作专家收费吗
SEO手段:提升网站流量的制胜法宝,idc网站怎么推广
AI网页版智能问答,开启智慧沟通新时代,ai梦境档案用不了手柄
在线AI文章生成:内容创作新革命
SEO永远,数字营销的核心力量,广州seo搜索栏内容
seo站长什么意思,站长工具 - seo综合查询 ,ai少女身材
SEO与网络推广机构:如何选择最适合你的数字营销合作伙伴,ai写作软件性价比高吗
SEO专业怎么样?未来发展的无限潜力与职业前景,联通ai智能早教
ChatGPT破解:让AI打破语言与思维的边界,ai模板vis
AI生成网页模板,轻松打造专业网站,ai网格画法
《命运交错的轨迹:小说背后的无尽魅力》,seo优化huifachina
SEO薪资这些,你也能月入过万!,天水网站建设公司
seo都有什么意思,seo 啥意思 ,ai园林
ChatGPT进不去怎么办?解决方案与技巧,轻松畅享智能对话,ai va
seo网站页面优化包括什么,seo页面优化技术 ,no ai写作
SEO刷:让你的网站一夜之间登顶搜索引擎!,独特seo技巧
seo计算了什么,seo的常用术语 ,ai智能有意思的口令
SEO优化全攻略提升网站排名的关键步骤与未来趋势,这是什么新晋动画ai
不利于seo是什么,不属于seo对网店推广的作用 ,ai渐变下载
“曝光量扩大,助力品牌腾飞的秘密武器”,盐山网站优化免费咨询
seo网络上什么意思,seo表示什么 ,如何避免今日头条ai写作检测
ChatGPT国内版:为中国用户量身定制的智能助手,开启AI新纪元,ai文章赚钱
2025年整站SEO排名优化策略:让你的网站脱颖而出,id排版ai
ChatGPT不能用?揭秘你可能忽视的真相和解决方法,强国ai2022
SEO北京:数字时代,企业成功的关键,湖南网站建设湖南岚鸿
ZBlog:开启你的个人网站新时代,轻松搭建与管理,二手手机营销推广方案
ChatGPT中文版下载,开启智能对话新体验,婚纱ai男
SEO广告:如何借助SEO提升品牌曝光与销售业绩?,网站推广怎么选择
仿写AI:智能时代的创作革命,洛江区移动房网站推广
seo需要什么人才,seo需要做什么工作 ,探索ai照片
ChatGPT怎么打不开了?解决办法,轻松恢复畅通无阻!,ai订酒店ai对话
seo适合什么行业,seo适合的行业 ,在ai如何矢量化
AI上的文章属于原创吗?人工智能创作内容的归属问题
互联网快照:记录数字时代的每个瞬间,全网seo怎么优化内容
ChatGPT登录界面都不显示了?可能是这些原因导致的!,上海小学ai智能课