bl双性强迫侵犯h_国产在线观看人成激情视频_蜜芽188_被诱拐的少孩全彩啪啪漫画

在MVC下怎么用XML實現breadcrumbs導航欄

小編給大家分享一下在MVC下怎么用XML實現breadcrumbs導航欄,希望大家閱讀完這篇文章之后都有所收獲,下面讓我們一起去探討吧!

專注于為中小企業提供成都網站設計、成都網站制作、外貿網站建設服務,電腦端+手機端+微信端的三站合一,更高效的管理,為中小企業十堰鄖陽免費做網站提供優質的服務。我們立足成都,凝聚了一批互聯網行業人才,有力地推動了上千企業的穩健成長,幫助中小企業通過網站建設實現規模擴充和轉變。

先看下樣子在MVC下怎么用XML實現breadcrumbs導航欄

像這種導航欄(breadcrumbs)在mvc下我們來實現他。我們采用XML來實現這個功能。

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

代碼如下

public static void RegisterRoutes(RouteCollection routes)          {              routes.IgnoreRoute("{resource}.axd/{*pathInfo}");              routes.MapRoute(               "inner",                                              // Route name               "resume/test/inner/{action}/{id}",                           // URL with parameters               new { controller = "inner", action = "Index", id = "" }  // Parameter defaults               );              routes.MapRoute(             "test",                                              // Route name             "resume/test/{action}/{id}",                           // URL with parameters             new { controller = "test", action = "Index", id = "" }  // Parameter defaults             );              routes.MapRoute(                  "Default",                                              // Route name                  "{controller}/{action}/{id}",                           // URL with parameters                  new { controller = "Home", action = "Index", id = "" },                  new { controller = "^(?!(test|inner)).*$", action = "^(?!test).*$" }              );            }

我們加了兩個規則

/resume/test

和/resume/test/inner

2.編寫用到的XML文件,注意是樹形結構的

在models寫個Navigator.xml

<?<?xml version="1.0" encoding="utf-8" ?> <node Title="首頁"  Description="潘峰的網站" Action="Index" Controller="Home">   <node Title="簡歷" Description="在線簡歷" Action="Index" Controller="Resume">     <node Title="Test" Description="Test" Action="Index" Controller="test">       <node Title="inner" Description="inner" Action="Index" Controller="inner">       </node>     </node>   </node> </node>

3.編寫我們的類文件來實現Navigator

在models寫個navigatorHelper.cs

using System;  using System.Collections.Generic;  using System.Linq;  using System.Web;  using System.Xml;  using System.Xml.Linq;  using System.Web.Routing;  using System.Web.Mvc;  using System.IO;  using System.Text;   namespace conansoft.Helpers  {      public static class MenuHelper      {          private static HttpServerUtilityBase Server = null;
  •         private static HttpRequestBase Request = null;  

  •         private static UrlHelper Url = null;  

  •         private static RouteValueDictionary RouteDictionary = null;  

  •         public static string Navigator(this HtmlHelper helper)  

  •         {  

  •             Server = helper.ViewContext.RequestContext.HttpContext.Server;  

  •             Request = helper.ViewContext.RequestContext.HttpContext.Request;  

  •             Url = new UrlHelper(helper.ViewContext.RequestContext);  

  •             RouteDictionary = helper.ViewContext.RequestContext.RouteData.Values;  

  •             string xmlPath = Server.MapPath(Url.Content("~/Models/Navigator.xml"));  

  •             XDocument doc = XDocument.Load(xmlPath);  

  •             XElement node = FindNode(doc.Root);  

  •             StringBuilder sb = new StringBuilder();  

  •             Stack s = new Stack();  

  •             while (node != null)  

  •             {  

  •                 s.Push(node);  

  •                 nodenode = node.Parent;  

  •             }  

  •             //輸出breadcrumbs.可以自行修改使之符合你的要求  

  •             while (s.Count() != 0)  

  •             {  

  •                 node = s.Pop();  

  •                 if (UrlEqual(node))  

  •                 {  

  •                     sb.AppendLine(string.Format("{0}", node.Attribute("Title").Value, node.Attribute("Description").Value));  

  •                 }  

  •                 else  

  •                 {  

  •                     sb.AppendLine(string.Format("{0}", node.Attribute("Title").Value,  

  •                         Url.Action(node.Attribute("Action").Value, node.Attribute("Controller").Value),  

  •                         node.Attribute("Description").Value));  

  •                     sb.AppendLine(" > ");  

  •                 }  

  •             }  

  •             return sb.ToString();  

  •         }  

  •  

  •         ///   

  •         /// 查找當前節點  

  •         ///   

  •         /// 當前節點  

  •         /// 找到返回,找不到為空  

  •         private static XElement FindNode(XElement e)  

  •         {  

  •             XElement result = e;  

  •               

  •             

  •             if (UrlEqual(e))  

  •             {  

  •                 return e;  

  •             }  

  •             else  

  •             {  

  •                 if (e.HasElements)  

  •                 {  

  •                     foreach (XElement ee in e.Elements())  

  •                     {  

  •                         result = FindNode(ee);  

  •                     }  

  •                 }  

  •                 else  

  •                 {  

  •                     return null;  

  •                 }  

  •                 return result;  

  •             }  

  •         }  

  •  

  •         ///   

  •         /// Url是否相等  

  •         ///   

  •         /// 節點  

  •         private static bool UrlEqual(XElement e)  

  •         {  

  •             string url1 = Url.Action(e.Attribute("Action").Value, e.Attribute("Controller").Value).ToLower();  

  •             string url2 = Url.RouteUrl(RouteDictionary).ToLower();  

  •             return url1 == url2;  

  •         }  

  •     }  

  • 解釋一下我們利用xml文件來實現breadcrumbs,并且我們用action和controller來判斷是否為當前路徑[UrlEqual]

    在網頁中加入

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

    好了效果如下在MVC下怎么用XML實現breadcrumbs導航欄

    看完了這篇文章,相信你對“在MVC下怎么用XML實現breadcrumbs導航欄”有了一定的了解,如果想了解更多相關知識,歡迎關注創新互聯行業資訊頻道,感謝各位的閱讀!

    當前題目:在MVC下怎么用XML實現breadcrumbs導航欄
    網站路徑:http://vcdvsql.cn/article34/ggpgse.html

    成都網站建設公司_創新互聯,為您提供外貿建站靜態網站網頁設計公司企業建站商城網站網站維護

    廣告

    聲明:本網站發布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網站立場,如需處理請聯系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經允許不得轉載,或轉載時需注明來源: 創新互聯

    綿陽服務器托管