资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

在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

                                

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导航栏
    URL网址:http://cdkjz.cn/article/ggpgse.html
    多年建站经验

    多一份参考,总有益处

    联系快上网,免费获得专属《策划方案》及报价

    咨询相关问题或预约面谈,可以通过以下方式与我们联系

    大客户专线   成都:13518219792   座机:028-86922220