zoukankan      html  css  js  c++  java
  • MVC小系列(十九)【mvc与站点地图】

    我的MvcSiteMap地图主要由实体文件,XML配置文件,C#调用文件组成,当然为了前台调用方法,可以为HtmlHelper添加一个扩展方法

    第一步 定义站点地图实体

     public class MvcSiteMap
        {
            [XmlAttribute]
            public int ID { get; set; }
            [XmlAttribute]
            public string Title { get; set; }
            [XmlAttribute]
            public string Url { get; set; }
            [XmlAttribute]
            public int ParnetID { get; set; }
            public MvcSiteMap Parent { get; set; }
        }
        public class MvcSiteMapList 
        {
            public List<MvcSiteMap> MvcSiteMaps { get; set; }
        }
    View Code

    第二步 做个示例的xml

    <?xml version="1.0" encoding="utf-8" ?>
    <MvcSiteMapList>
      <MvcSiteMaps>
        <MvcSiteMap Title = ""  Url = "#" ID = "1" ParnetID = "0"></MvcSiteMap>
        <MvcSiteMap Title = "测试网站"  Url = "#" ID = "2" ParnetID = "1"></MvcSiteMap>
        <MvcSiteMap Title = "首页123sadfasdfds"  Url = "/" ID = "3" ParnetID = "2"></MvcSiteMap>
      </MvcSiteMaps>
    </MvcSiteMapList>

    第三步:地图核心代码

     public class MvcSiteMapFactory
        {
            private static List<MvcSiteMap> siteMapList
            {
                get
                {
                    if (string.IsNullOrWhiteSpace(SiteMapString))
                        throw new ArgumentException("请为在web.config中配置SiteMapString节点,以支持网站地图功能");
    
                    return ConfigCache.ConfigFactory.Instance.GetConfig<MvcSiteMapList>(System.Web.HttpContext.Current.Server.MapPath(SiteMapString)).MvcSiteMaps;
                }
            }
    
            private static string SiteMapString = System.Configuration.ConfigurationManager.AppSettings["SiteMapString"] ?? string.Empty;
    
            /// <summary>
            /// 生成站点地图
            /// </summary>
            /// <param name="url"></param>
            /// <returns></returns>
            public static MvcHtmlString GeneratorSiteMap(string url)
            {
                StringBuilder str = new StringBuilder();
                List<string> pathList = new List<string>();
                MvcSiteMap current = GetSiteMap(url);
                GetFather(current, pathList);
                pathList.Reverse();
                pathList.ForEach(i =>
                {
                    str.AppendFormat("<span style='padding:0 5px;'>{0}</span>>", i);
                });
    
                string result = str.ToString();
                if (!string.IsNullOrWhiteSpace(result))
                    result = result.Remove(str.ToString().Length - 1);
    
                return MvcHtmlString.Create(result);
            }
    
            static MvcSiteMap GetSiteMap(string url)
            {
                return siteMapList.FirstOrDefault(i => i.Url == url);
            }
            /// <summary>
            /// 递归找老祖宗
            /// </summary>
            /// <param name="father"></param>
            static void GetFather(MvcSiteMap father, List<string> pathList)
            {
                if (father != null)
                {
                    pathList.Add(string.Format("<a href={0}>{1}</a>", father.Url, father.Title));
                    father.Parent = siteMapList.FirstOrDefault(i => i.ID == father.ParnetID);
                    GetFather(father.Parent, pathList);
                }
            }
        }
    View Code

    第四步:做个扩展

     /// <summary>
        /// 站点地图扩展
        /// </summary>
        public static class MvcSiteMapExtensions
        {
            public static MvcHtmlString GeneratorSiteMap(this HtmlHelper html, string url)
            {
                return MvcSiteMapFactory.GeneratorSiteMap(url);
            }
        }

    第五步:前台调用

    <div class="sitemap">
       @Html.GeneratorSiteMap(Request.Url.AbsolutePath)
     </div>
  • 相关阅读:
    nodejs安装
    Python基本知识3----序列
    jdk环境变量配置
    sublime text3插件的安装
    QTP基本方法4------手动写入信息到测试结果报告中
    QTP基本方法3-----截屏
    QTP基本方法2------截取字符串
    QTP基本方法
    python文件操作指令
    XSStrike工具的安装使用
  • 原文地址:https://www.cnblogs.com/niuzaihenmang/p/5626685.html
Copyright © 2011-2022 走看看