爱上MVC3系列~开发一个站点地图(俗称面包屑)

回到目录

原来早在webform控件时代就有了SiteMap这个东西,而进行MVC时代后,我们也希望有这样一个东西,它为我们提供了不少方便,如很方便的实现页面导航的内容修改,页面导航的样式换肤等.

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

实体部分

    /// <summary>
/// mvc站点地图(面包屑)
/// </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 = "根" Url = "#" ID = "" ParnetID = ""></MvcSiteMap>
<MvcSiteMap Title = "测试网站" Url = "#" ID = "" ParnetID = ""></MvcSiteMap>
<MvcSiteMap Title = "首页123sadfasdfds" Url = "/" ID = "" ParnetID = ""></MvcSiteMap>
</MvcSiteMaps>
</MvcSiteMapList>

C#核心代码

    /// <summary>
/// 站点地图工厂
/// </summary>
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 - ); 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>
/// 站点地图扩展
/// </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>

运行效果如图

aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAXYAAAB4CAIAAABpQU72AAAIbklEQVR4nO3cv6rbSBTHccE+hB5DhR/iwlapw6pRlyZPEVjcqEqb+wxiQU1Id4t0qcyC+xAM22VhYSttMdboaP5Jsn0s+e73gwiJriWNfDU/nxnJyX77/Q8WFhYWpSVbvQUsLCyveCFiWFhYFBcihoWFRXEhYlhYWBSXcMS8//j565/ff/7zbwcAVwhEzPuPnwkXADcRiJivf35fu1UAXolAxHz/6+farQLwSgQiZu0mAXg9iBgAiogYAIqIGACKiBgAiogYAIqIGACKiBgAiogYAIqIGACKiBgAiogYAIqIGACKiBgAiogYAIqIGACKiBgAiogYAIqIGACKiBgAiogYAIqIGACKiBgAiogYAIqIGACKiBgAiogYAIqIGACK1o+YX3792/5FLnrHiu1c/tRpjL84jfdP5xpv3n5JrHnz9kt6ub4BwK1sK2L8lZftLZZWiWhY1IDEK28Vjk6mzHwls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kl5Xb+C0tqS535AswaTv/XwyAV4iIAaCIiAGgiIgBoIiIAaCIiAGgiIgBoIiIAaCIiAGgiIgBoIiIAaCIiAGgiIgBoIiIAaCIiAGgiIgBoIiIAaCIiAGgiIgBoIiIAaCIiAGgiIgBoIiIAaCIiAGgiIgBoIiIAaCIiAGgiIgBoIiIAaCIiAGgiIgBoIiIAaCIiAGgiIgBoIiIAaCIiAGg6D9ZzvuXwpMNlgAAAABJRU5ErkJggg==" alt="" />

回到目录

上一篇:Java Web 乱码


下一篇:C#开源磁盘/内存缓存引擎