• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

asp.net的cms 核心代码篇

asp 搞代码 4年前 (2022-01-03) 44次浏览 已收录 0个评论

第一篇:我简略描述了一下我的cms标签所表示的含义;anCMS(c#版)第一篇绑定数据;第二篇:我将展示了标签背后真正运行的代码,本文将介绍asp.net的cms 核心代码篇

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

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

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

代码如下:
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace an.helper
{
///

/// 查找html页面中可以转换成aspx页面的方法 的正则表达式
///

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”;
///

/// 所有方法的入口,第一次运行时遍历该方法。
///

public static string AllMethodContentRegex = “[\\s\\S]*?“;
public static List AllMethodContent(string content)
{
return Method.RegexStr(AllMethodContentRegex, content);
}
///

/// 获取所有的方法
///

public static string AllMethodRegex = ““;
public static List AllMethod(string content)
{
return Method.RegexStr(AllMethodRegex, content);
}

///

/// 获取方法的函数名称
///

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:", "");
}
///

/// 获取方法的字段和值
///

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 = “”;
public static string[] JsInclude(string html)
{
string js = “”;
List list_include = Method.RegexStr(JsIncludeRegex, html);
foreach (string inc in list_include)
{
List includeList = Method.RegexStr(@”\w+(?=\.js)”, inc);
if (includeList != null)
{
js += includeList[0] + “(); “;
html = html.Replace(inc, “ “);
}
}
return new string[] { js, html };
}
///

/// 占位符
///

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, “”);
}
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, “”);
}
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”}]
//
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

    [Category=$category$][15]
    //
    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 = “(?<=";
    }
    public static string ServerFunction(string content, string fnName)
    {
    return “”;
    }

    }

    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;}
    }
    }
    ///

    /// html和aspx的方法名称对应替换
    ///

    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 list_AllMethodContent = HtmlToAspxMethod.AllMethodContent(html);
    string load = “”;
    int i = 0;
    foreach (string allMethodContent in list_AllMethodContent)
    {
    i++;
    string allMethod = HtmlToAspxMethod.AllMethod(allMethodContent)[0];//
    string methodName = HtmlToAspxMethod.MethodName(allMethod);//方法名称<!–{an:list
    Hashtable methodFiled = HtmlToAspxMethod.MethodFiled(allMethod);//filed=[title] sort=[time desc]
    string content = allMethodContent.Replace(allMethod, “”).Replace(““, “”);
    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”, “”);
    //html = html.Replace(“$title$”, “”).//页面标题
    // Replace(“$keywords$”, “”).//页面关键词
    // Replace(“$description$”, “”).//页面描述
    // Replace(“$domain$”, “”).//网站域名
    // Replace(“$categoryname$”, “”).//分类名称
    // Replace(“$contact$”, “\”+An_Contact+\””).//联系我们
    // Replace(“$id$”, “An_ID”).//明细ID
    // Replace(“$category$”, “An_CategoryID”).//分类ID
    // Replace(“$tongji$”, “”).
    // Replace(“$keyword$”, “\”+An_KeyWord+\””);//搜索的关键词
    html = HtmlToAspxMethod.UrlRou来源gao@!dai!ma.com搞$$代^@码网te(html);
    html = HtmlToAspxMethod.Paging(html);
    html = HtmlToAspxMethod.FnSplit(html);

    return html;
    }

    }
    }

    呵呵,会不会太肤浅了。

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

    我要去完善我的CMS啦。

    以上就是asp.net的cms 核心代码篇的详细内容,更多请关注gaodaima搞代码网其它相关文章!


    搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
    转载请注明原文链接:asp.net的cms 核心代码篇

    喜欢 (0)
    [搞代码]
    分享 (0)
    发表我的评论
    取消评论

    表情 贴图 加粗 删除线 居中 斜体 签到

    Hi,您需要填写昵称和邮箱!

    • 昵称 (必填)
    • 邮箱 (必填)
    • 网址