自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

在MVC下用XML實(shí)現(xiàn)breadcrumbs導(dǎo)航欄

開發(fā) 后端
本文將介紹在ASP.NET MVC下用XML實(shí)現(xiàn)breadcrumbs導(dǎo)航欄。這是網(wǎng)站開發(fā)中比較常用的一種導(dǎo)航欄樣式,類似于蘋果MAC界面的樣式。

先看下樣子導(dǎo)航欄樣式

像這種導(dǎo)航欄(breadcrumbs)在mvc下我們來實(shí)現(xiàn)他。我們采用XML來實(shí)現(xiàn)這個(gè)功能。

1.首先做個(gè)準(zhǔn)備,我們編寫rounting規(guī)則(順便提一句,我們要用到rounting功能,所以規(guī)則必須寫正確,不然出不來喔)

代碼如下

  1. public static void RegisterRoutes(RouteCollection routes)  
  2.         {  
  3.             routes.IgnoreRoute("{resource}.axd/{*pathInfo}");  
  4.             routes.MapRoute(  
  5.              "inner",                                              // Route name  
  6.              "resume/test/inner/{action}/{id}",                           // URL with parameters  
  7.              new { controller = "inner"action = "Index"id = "" }  // Parameter defaults  
  8.              );  
  9.             routes.MapRoute(  
  10.            "test",                                              // Route name  
  11.            "resume/test/{action}/{id}",                           // URL with parameters  
  12.            new { controller = "test"action = "Index"id = "" }  // Parameter defaults  
  13.            );  
  14.             routes.MapRoute(  
  15.                 "Default",                                              // Route name  
  16.                 "{controller}/{action}/{id}",                           // URL with parameters  
  17.                 new { controller = "Home"action = "Index"id = "" },  
  18.                 new { controller = "^(?!(test|inner)).*$"action = "^(?!test).*$" }  
  19.             );    
  20.         } 

我們加了兩個(gè)規(guī)則

/resume/test

和/resume/test/inner

2.編寫用到的XML文件,注意是樹形結(jié)構(gòu)的

在models寫個(gè)Navigator.xml

  1. <?xml version="1.0" encoding="utf-8" ?> 
  2. <node Title="首頁"  Description="潘峰的網(wǎng)站" Action="Index" Controller="Home"> 
  3.   <node Title="簡歷" Description="在線簡歷" Action="Index" Controller="Resume"> 
  4.     <node Title="Test" Description="Test" Action="Index" Controller="test"> 
  5.       <node Title="inner" Description="inner" Action="Index" Controller="inner"> 
  6.       </node> 
  7.     </node> 
  8.   </node> 
  9. </node> 

3.編寫我們的類文件來實(shí)現(xiàn)Navigator

在models寫個(gè)navigatorHelper.cs

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Web;  
  5. using System.Xml;  
  6. using System.Xml.Linq;  
  7. using System.Web.Routing;  
  8. using System.Web.Mvc;  
  9. using System.IO;  
  10. using System.Text;  
  11.  
  12. namespace conansoft.Helpers  
  13. {  
  14.     public static class MenuHelper  
  15.     {  
  16.         private static HttpServerUtilityBase Server = null;  
  17.         private static HttpRequestBase Request = null;  
  18.         private static UrlHelper Url = null;  
  19.         private static RouteValueDictionary RouteDictionary = null;  
  20.         public static string Navigator(this HtmlHelper helper)  
  21.         {  
  22.             Server = helper.ViewContext.RequestContext.HttpContext.Server;  
  23.             Request = helper.ViewContext.RequestContext.HttpContext.Request;  
  24.             Url = new UrlHelper(helper.ViewContext.RequestContext);  
  25.             RouteDictionary = helper.ViewContext.RequestContext.RouteData.Values;  
  26.             string xmlPath = Server.MapPath(Url.Content("~/Models/Navigator.xml"));  
  27.             XDocument doc = XDocument.Load(xmlPath);  
  28.             XElement node = FindNode(doc.Root);  
  29.             StringBuilder sb = new StringBuilder();  
  30.             Stack s = new Stack();  
  31.             while (node != null)  
  32.             {  
  33.                 s.Push(node);  
  34.                 nodenode = node.Parent;  
  35.             }  
  36.             //輸出breadcrumbs.可以自行修改使之符合你的要求  
  37.             while (s.Count() != 0)  
  38.             {  
  39.                 node = s.Pop();  
  40.                 if (UrlEqual(node))  
  41.                 {  
  42.                     sb.AppendLine(string.Format("{0}", node.Attribute("Title").Value, node.Attribute("Description").Value));  
  43.                 }  
  44.                 else  
  45.                 {  
  46.                     sb.AppendLine(string.Format("{0}", node.Attribute("Title").Value,  
  47.                         Url.Action(node.Attribute("Action").Value, node.Attribute("Controller").Value),  
  48.                         node.Attribute("Description").Value));  
  49.                     sb.AppendLine(" > ");  
  50.                 }  
  51.             }  
  52.             return sb.ToString();  
  53.         }  
  54.  
  55.         ///   
  56.         /// 查找當(dāng)前節(jié)點(diǎn)  
  57.         ///   
  58.         /// 當(dāng)前節(jié)點(diǎn)  
  59.         /// 找到返回,找不到為空  
  60.         private static XElement FindNode(XElement e)  
  61.         {  
  62.             XElement result = e;  
  63.               
  64.             
  65.             if (UrlEqual(e))  
  66.             {  
  67.                 return e;  
  68.             }  
  69.             else  
  70.             {  
  71.                 if (e.HasElements)  
  72.                 {  
  73.                     foreach (XElement ee in e.Elements())  
  74.                     {  
  75.                         result = FindNode(ee);  
  76.                     }  
  77.                 }  
  78.                 else  
  79.                 {  
  80.                     return null;  
  81.                 }  
  82.                 return result;  
  83.             }  
  84.         }  
  85.  
  86.         ///   
  87.         /// Url是否相等  
  88.         ///   
  89.         /// 節(jié)點(diǎn)  
  90.         private static bool UrlEqual(XElement e)  
  91.         {  
  92.             string url1 = Url.Action(e.Attribute("Action").Value, e.Attribute("Controller").Value).ToLower();  
  93.             string url2 = Url.RouteUrl(RouteDictionary).ToLower();  
  94.             return url1 == url2;  
  95.         }  
  96.     }  

解釋一下我們利用xml文件來實(shí)現(xiàn)breadcrumbs,并且我們用action和controller來判斷是否為當(dāng)前路徑[UrlEqual]

在網(wǎng)頁中加入

  1. <%=Html.Navigator() %> 
<%=Html.Navigator() %>

好了效果如下效果圖

我的網(wǎng)站

[[3800]]

實(shí)例

【編輯推薦】

  1. 亮劍.NET:圖解ASP.NET網(wǎng)站開發(fā)實(shí)戰(zhàn)
  2. 作為ASP.NET開發(fā)人員必須養(yǎng)成的編程習(xí)慣
  3. 視頻教程:ASP.NET Web開發(fā)詳解
  4. 教你如何配置Struts2 web.xml文件
  5. 在Spring中裝配bean的基本xml配置
責(zé)任編輯:彭凡 來源: cnblogs
相關(guān)推薦

2012-04-28 11:07:15

2023-10-23 08:48:04

CSS寬度標(biāo)題

2016-12-07 10:02:54

移動(dòng)應(yīng)用開發(fā)底部導(dǎo)航android

2016-12-07 10:27:16

移動(dòng)應(yīng)用開發(fā)底部導(dǎo)航android

2022-11-15 18:31:37

React

2016-12-07 10:18:44

移動(dòng)應(yīng)用開發(fā)底部導(dǎo)航android

2009-05-18 10:11:06

MVCXML動(dòng)態(tài)表單

2021-01-28 06:11:40

導(dǎo)航組件Sidenav Javascript

2016-12-07 10:32:14

移動(dòng)應(yīng)用開發(fā)底部導(dǎo)航android

2016-12-07 10:58:35

移動(dòng)應(yīng)用開發(fā)底部導(dǎo)航android

2009-01-03 14:39:04

ibmdwDojoMVC

2021-02-20 18:00:26

rangerLinux

2009-12-28 17:17:52

WPF導(dǎo)航

2009-02-12 09:55:22

AjaxMVCDojo

2009-04-17 09:30:33

Firefox插件瀏覽器

2023-06-06 15:38:28

HTMLCSS開發(fā)

2009-03-31 13:12:05

ASP.NETMVC表單驗(yàn)證

2021-01-21 05:55:24

Linux運(yùn)維Linux系統(tǒng)

2015-07-30 14:43:04

導(dǎo)航欄iOS開發(fā)

2011-09-08 13:15:00

UbuntuFoxit Reade
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)