音效素材网提供各类素材,打造精品素材网站!

站内导航 站长工具 投稿中心 手机访问

音效素材

asp.net的cms 核心代码篇
日期:2021-09-07 21:27:51   来源:脚本之家

第一篇,我简略描述了一下我的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啦。

    您感兴趣的教程

    在docker中安装mysql详解

    本篇文章主要介绍了在docker中安装mysql详解,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编...

    详解 安装 docker mysql

    win10中文输入法仅在桌面显示怎么办?

    win10中文输入法仅在桌面显示怎么办?

    win10系统使用搜狗,QQ输入法只有在显示桌面的时候才出来,在使用其他程序输入框里面却只能输入字母数字,win10中...

    win10 中文输入法

    一分钟掌握linux系统目录结构

    这篇文章主要介绍了linux系统目录结构,通过结构图和多张表格了解linux系统目录结构,感兴趣的小伙伴们可以参考一...

    结构 目录 系统 linux

    PHP程序员玩转Linux系列 Linux和Windows安装

    这篇文章主要为大家详细介绍了PHP程序员玩转Linux系列文章,Linux和Windows安装nginx教程,具有一定的参考价值,感兴趣...

    玩转 程序员 安装 系列 PHP

    win10怎么安装杜比音效Doby V4.1 win10安装杜

    第四代杜比®家庭影院®技术包含了一整套协同工作的技术,让PC 发出清晰的环绕声同时第四代杜比家庭影院技术...

    win10杜比音效

    纯CSS实现iOS风格打开关闭选择框功能

    这篇文章主要介绍了纯CSS实现iOS风格打开关闭选择框,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作...

    css ios c

    Win7如何给C盘扩容 Win7系统电脑C盘扩容的办法

    Win7如何给C盘扩容 Win7系统电脑C盘扩容的

    Win7给电脑C盘扩容的办法大家知道吗?当系统分区C盘空间不足时,就需要给它扩容了,如果不管,C盘没有足够的空间...

    Win7 C盘 扩容

    百度推广竞品词的投放策略

    SEM是基于关键词搜索的营销活动。作为推广人员,我们所做的工作,就是打理成千上万的关键词,关注它们的质量度...

    百度推广 竞品词

    Visual Studio Code(vscode) git的使用教程

    这篇文章主要介绍了详解Visual Studio Code(vscode) git的使用,小编觉得挺不错的,现在分享给大家,也给大家做个参考。...

    教程 Studio Visual Code git

    七牛云储存创始人分享七牛的创立故事与

    这篇文章主要介绍了七牛云储存创始人分享七牛的创立故事与对Go语言的应用,七牛选用Go语言这门新兴的编程语言进行...

    七牛 Go语言

    Win10预览版Mobile 10547即将发布 9月19日上午

    微软副总裁Gabriel Aul的Twitter透露了 Win10 Mobile预览版10536即将发布,他表示该版本已进入内部慢速版阶段,发布时间目...

    Win10 预览版

    HTML标签meta总结,HTML5 head meta 属性整理

    移动前端开发中添加一些webkit专属的HTML5头部标签,帮助浏览器更好解析HTML代码,更好地将移动web前端页面表现出来...

    移动端html5模拟长按事件的实现方法

    这篇文章主要介绍了移动端html5模拟长按事件的实现方法的相关资料,小编觉得挺不错的,现在分享给大家,也给大家...

    移动端 html5 长按

    HTML常用meta大全(推荐)

    这篇文章主要介绍了HTML常用meta大全(推荐),文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参...

    cdr怎么把图片转换成位图? cdr图片转换为位图的教程

    cdr怎么把图片转换成位图? cdr图片转换为

    cdr怎么把图片转换成位图?cdr中插入的图片想要转换成位图,该怎么转换呢?下面我们就来看看cdr图片转换为位图的...

    cdr 图片 位图

    win10系统怎么录屏?win10系统自带录屏详细教程

    win10系统怎么录屏?win10系统自带录屏详细

    当我们是使用win10系统的时候,想要录制电脑上的画面,这时候有人会想到下个第三方软件,其实可以用电脑上的自带...

    win10 系统自带录屏 详细教程

    + 更多教程 +
    ASP编程JSP编程PHP编程.NET编程python编程