1 2 3 4 5 6 7 | public static void RegisterRoutes(RouteCollection routes) { routes.IgnoreRoute( "{resource}.axd/{*pathInfo}" ); string [] nameSpaces = { "TestMvc3.Controllers" }; routes.MapRoute( "default2" , "Youku/Moive" , new { Controller = "Moive" , Action = "Index" }, nameSpaces); routes.MapRoute( "default" , "{Controller}/{Action}" , new { Controller = "Home" , Action = "Index" },nameSpaces); } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | using System.Web.Mvc; namespace TestMvc3.Areas.UserCenter { public class UserCenterAreaRegistration : AreaRegistration { public override string AreaName { get { return "UserCenter" ; } } public override void RegisterArea(AreaRegistrationContext context) { context.MapRoute( "UserCenter_default" , "UserCenter/{controller}/{action}" , new { action = "Index" } ); } } } |
1 2 3 4 5 6 7 8 9 | <? xml version = "1.0" encoding = "utf-8" ?> < node title = "首页" area = "" action = "Index" controller = "Home" > < node title = "电影" area = "" action = "Index" controller = "Moive" > < node title = "详细页" area = "" action = "Detail" controller = "Moive" /> </ node > < node title = "用户中心" area = "UserCenter" action = "Index" controller = "Home" /> </ node > |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 | using System; using System.Collections.Generic; using System.Text; using System.Web; using System.Web.Mvc; using System.Xml.Linq; using System.Web.Routing; namespace TestMvc3 { public class MvcSiteMap { private static XDocument doc = XDocument.Load(HttpContext.Current.Server.MapPath( @"~/SiteMap.xml" )); private UrlHelper url = null ; private string currentURL = "" ; public MvcSiteMap() { url = new UrlHelper(HttpContext.Current.Request.RequestContext); currentURL = url.RouteUrl(HttpContext.Current.Request.RequestContext.RouteData.Values).ToLower(); } public MvcHtmlString Navigator() { XElement c = FindNode(doc.Root); Stack<XElement> temp = GetPath(c); return MvcHtmlString.Create(BuildPathString(temp)); } private string GetNodeUrl(XElement c) { return url.Action(c.Attribute( "action" ).Value, c.Attribute( "controller" ).Value, new { area = c.Attribute( "area" ).Value }); } private string BuildPathString(Stack<XElement> m) { StringBuilder sb = new StringBuilder(); TagBuilder tb; TagBuilder tc = new TagBuilder( "span" ); tc.SetInnerText( ">>" ); string sp = tc.ToString(); int count = m.Count; for ( int x = 1; x <= count; x++) { XElement c = m.Pop(); if (x == count) { tb = new TagBuilder( "span" ); } else { tb = new TagBuilder( "a" ); tb.MergeAttribute( "href" , GetNodeUrl(c)); } tb.SetInnerText(c.Attribute( "title" ).Value); sb.Append(tb.ToString()); sb.Append(sp); } return sb.ToString(); } private Stack<XElement> GetPath(XElement c) { Stack<XElement> temp = new Stack<XElement>(); while (c != null ) { temp.Push(c); c = c.Parent; } return temp; } private bool IsUrlEqual(XElement c) { string a = GetNodeUrl(c).ToLower(); return a == currentURL; } private XElement RecursiveNode(XElement node) { foreach (XElement c in node.Elements()) { if (IsUrlEqual(c) == true ) { return c; } else { XElement x = RecursiveNode(c); if (x != null ) { return x; } } } return null ; } private XElement FindNode(XElement node) { if (IsUrlEqual(node) == true ) { return node; } else { return RecursiveNode(node); } } } } |
联系客服