愛上(shang)MVC3系列~開發一個(ge)站點地圖(俗(su)稱面包屑)
原來早在webform控件時代就有了SiteMap這個東西(xi),而進行MVC時代后,我們(men)也希望有這樣一(yi)個東西(xi),它為我們(men)提供了不少方便,如很(hen)方便的實(shi)現頁面(mian)導(dao)航的內容(rong)修改(gai),頁面(mian)導(dao)航的樣式(shi)換膚等(deng).
我的MvcSiteMap主要由實體文(wen)件(jian),XML配(pei)置文(wen)件(jian),C#調用文(wen)件(jian)組成,當然為了前(qian)臺(tai)使用方(fang)便,可以為HtmlHelper添加(jia)一(yi)個(ge)擴展方(fang)法.
實體部分
/// <summary> /// mvc站點地圖(tu)(面包屑) /// </summary> 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 : IConfiger { public List<MvcSiteMap> MvcSiteMaps { get; set; } }
XML部分代碼
<?xml version="1.0" encoding="utf-8"?> <MvcSiteMapList> <MvcSiteMaps> <MvcSiteMap Title = "根(gen)" Url = "#" ID = "1" ParnetID = "0"></MvcSiteMap> <MvcSiteMap Title = "測試(shi)網站" Url = "#" ID = "2" ParnetID = "1"></MvcSiteMap> <MvcSiteMap Title = "首(shou)頁123sadfasdfds" Url = "/" ID = "3" ParnetID = "2"></MvcSiteMap> </MvcSiteMaps> </MvcSiteMapList>
C#核心代碼
/// <summary> /// 站點地(di)圖(tu)工廠 /// </summary> public class MvcSiteMapFactory { private static List<MvcSiteMap> siteMapList { get { if (string.IsNullOrWhiteSpace(SiteMapString)) throw new ArgumentException("請(qing)為在web.config中配(pei)置(zhi)SiteMapString節點,以支持(chi)網站地圖功能"); 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> /// 生成(cheng)站點地圖 /// </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); } } }
添加一個擴展方法
/// <summary> /// 站(zhan)點地(di)圖(tu)擴展 /// </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>