C# 实现网页内容页与母版合并及数据自动绑定

文章介绍了如何使用C#代码在Web开发中合并HTML母版页,包括头部和内容区域,同时管理缓存,以提高页面更新效率。作者通过`XHtmlAction`类操作XML文档,实现动态内容替换和缓存刷新。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 将内容页合并到母版生成完整网页,方便更新页面公共部分如导航条侧边栏等,只需更改母版页所有使用母版页的内容页面都会更新

 将head合并到母版的head中,将contentId的内容合并到母版的同名母版页位置

<master master="../masters/master.html">
    <head>
        <style>
            .test {
            }
        </style>
    </head>
    <div contentId="maincontent" style="font-size:20px;">
        test
    </div>
</master>
<!DOCTYPE html>
<html>
<head>
    
</head>
<body class="layui-layout-body">
    <div lay-server-options="{type:'user',name:'test123'}">
        {{d.name}}>cache
        <div lay-server-options="{type:'user',name:'test123-1'}">{{d.name}}
            <div lay-server-options="{type:'user',name:'test123-1-1'}">{{d}}
                <div lay-server-options="{type:'user',name:'test123-1-1-1'}">{{d.name}}</div>
            </div>
        </div>
    </div>
    <div contentId="maincontent"></div>
    <div contentId="subcontent"></div>
</body>
</html>

 合并后

<!DOCTYPE html>
<html>
<head>
    <style>
        .test {
        }
    </style>
</head>
<body class="layui-layout-body">
    <div> test123>cache 
        <div>test123-1 
            <div>test123-1-1
                <div>test123-1-1-1</div>
            </div>
        </div>
    </div>
    <div style="font-size:20px;">
        test
    </div>
    <div contentId="subcontent"></div>
</body>
</html>

C# 代码

//缓存时长
private static readonly XmlCacheLevel cacheLevel = XmlCacheLevel.NoCache;

private static string CalcPath(string basePath, string targetPath)
{
    try
    {
        if (!(basePath.EndsWith("\\") || basePath.EndsWith("/")))
        {
            if (Path.GetFileName(basePath).IndexOf(".") != -1)
            {
                basePath = Path.GetDirectoryName(basePath);
            }
        }
        return Path.GetFullPath(Path.Combine(basePath, targetPath));
    }
    catch
    {
        throw new Exception("Error File Path");
    }
}
//合并母版
private static void CombineMaster(XHtmlAction view, string viewPath)
{
    XmlNode root = view.XmlDoc.DocumentElement;
    if (root != null && root.Attributes["master"] != null)
    {
        string masterPath = root.Attributes["master"].Value;
        XHtmlAction masterView = new XHtmlAction();
        masterPath = CalcPath(viewPath, masterPath);
        if (masterView.Load(masterPath, cacheLevel, true))
        {
            foreach (XmlNode node in root.ChildNodes)
            {
                if (node.Name.Equals("head", StringComparison.OrdinalIgnoreCase)||
                    node.Name.Equals("body", StringComparison.OrdinalIgnoreCase))
                {
                    XmlNodeList mNodes = masterView.GetList(node.Name.ToLower());
                    if (mNodes.Count > 0)
                        mNodes[0].InnerXml += node.InnerXml;
                }
                else
                {
                    XmlAttribute nodeAttr = node.Attributes["contentId"];
                    if (nodeAttr != null)
                    {
                        string toContentID = nodeAttr.Value;
                        if (!String.IsNullOrEmpty(toContentID))
                        {
                            var cont = masterView.GetList("*", "contentId", toContentID);
                            if (cont.Count>0)
                            {
                                node.Attributes.Remove(node.Attributes["contentId"]);
                                masterView.ReplaceNode(node, cont[0]);
                            }
                        }
                    }
                }
            }
            view.LoadXml(masterView.OutXml);
        }
    }
}


public static XHtmlAction Create(string fullPath)
{
    XHtmlAction view = new XHtmlAction(true, false);
    if (view.Load(fullPath, cacheLevel, true))
    {
        if (!view.IsLoadFromCache)
        {
            //母版页替换
            CombineMaster(view, fullPath);
            view.RefleshCache(); //更新缓存
        }
        //加载控件数据
        LoadViewData(view);
    }
    return view;
}

 使用的CYQ.Data.Xml; 中的XHtmlAction 处理html数据

CYQ.Data 获取地址

https://github.com/cyq1162/cyqdata

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值