日韩无码专区无码一级三级片|91人人爱网站中日韩无码电影|厨房大战丰满熟妇|AV高清无码在线免费观看|另类AV日韩少妇熟女|中文日本大黄一级黄色片|色情在线视频免费|亚洲成人特黄a片|黄片wwwav色图欧美|欧亚乱色一区二区三区

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時(shí)間:8:30-17:00
你可能遇到了下面的問題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營(yíng)銷解決方案
在MVC下用XML實(shí)現(xiàn)breadcrumbs導(dǎo)航欄

先看下樣子

專注于為中小企業(yè)提供成都網(wǎng)站設(shè)計(jì)、網(wǎng)站制作服務(wù),電腦端+手機(jī)端+微信端的三站合一,更高效的管理,為中小企業(yè)靈武免費(fèi)做網(wǎng)站提供優(yōu)質(zhì)的服務(wù)。我們立足成都,凝聚了一批互聯(lián)網(wǎng)行業(yè)人才,有力地推動(dòng)了超過千家企業(yè)的穩(wěn)健成長(zhǎng),幫助中小企業(yè)通過網(wǎng)站建設(shè)實(shí)現(xiàn)規(guī)模擴(kuò)充和轉(zhuǎn)變。

像這種導(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.  version="1.0" encoding="utf-8" ?> 
  2.  Title="首頁"  Description="潘峰的網(wǎng)站" Action="Index" Controller="Home"> 
  3.    Title="簡(jiǎn)歷" Description="在線簡(jiǎn)歷" Action="Index" Controller="Resume"> 
  4.      Title="Test" Description="Test" Action="Index" Controller="test"> 
  5.        Title="inner" Description="inner" Action="Index" Controller="inner"> 
  6.        
  7.      
  8.    
  9.  

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]]


網(wǎng)站欄目:在MVC下用XML實(shí)現(xiàn)breadcrumbs導(dǎo)航欄
鏈接URL:http://m.5511xx.com/article/djphipp.html