Implement sitemap navigation __.net in asp.net mvc mode

Source: Internet
Author: User

Now all kinds of navigation bar, navigation menu has become an indispensable part of the site, in WebForms, the general use of SiteMapPath to achieve the site map navigation function. SiteMapPath server controls can still work in MVC mode, but it is not clear that the C-V structure is being separated, and that the SiteMapPath default XML file format has not met the rules for HTTP requests in MVC. So it is necessary to do a navigation bar class that is used under MVC and that conforms to the MVC design specification to replace the previous SiteMapPath application in MVC.



First look at the project structure as shown:



1: Adding routing Rules

In Global.asax:
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);
        }
2: Add a zone usercenter and add routing rules

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"}
            );
        }
    }
}

3: Building the XML file for navigation

We create sitemap.xml files in the root directory of the site to write navigation rules. The area attribute inside is to deal with the condition of the existing region.

<?xml version= "1.0" encoding= "Utf-8"?> <node title=


"homepage" area= "" action= "index" controller= "Home" >
    <node title= "movie" Area= "action=" Index "controller=" moive ">
        <node title=" Detail page "area=" "action=" Detail "controller=" moive "/> </node> <node title=" User Center Area= "usercenter" action= "
    Index" Controller= "Home"/>
</node>

4: Write the class Mvcsitemap class for navigation, which traverses the Sitemap.xml file and generates the navigation links.

Its core is to use the Urlhelper class action method and the RouteLink method to compare the current XML node properties generated by the link and the current request generated by the same link. There is no way to compare the concatenation of controller and action strings, because there may be a rewritten routing rule. The code for the class is as follows:

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.c Urrent.

        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 Stringbuil
            Der ();
            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 to 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); }
        }

    }
}

This class is typically used in the template page when used.



Effect Chart:








Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.