I. use ASP. NET WebForm developers I want to use Web. sitemap server controls. This plug-in brings us a great feel, greatly improving the development efficiency, and greatly improving future maintenance. But with ASP. net mvc, this plug-in does not seem to work very well. So there is a very useful mvc sitemap plug-in mvcsitemapprovider on the Internet. But today, Xiaosheng also wrote his own case, mvc Web. sitemap cross-Areas, mvc Web. sitemap is a cross-namespace and only available for learning. Do not spray it on Daniel. younger brother is just getting started! The Code is as follows:
Step 2
1. Create a Web. sitemap file. The url contains the Controller + Action
2. Create a new class GetMvcSiteMap. cs. If cache is used, add
Using System; using System. Collections. Generic; using System. Linq; using System. Web; using System. Data; public class MVCSiteMap {///
/// Obtain siteMap ////// The url of the current page, case sensitive, or Request. Url, but the format must be well processed/// Return a string of html code and read the html page directly. Public static string GetMvcSiteMap (string CurrentPageUrl) {DataSet ds = new DataSet (); ds. ReadXml (HttpContext. Current. Server. MapPath ("~ /Web. sitemap "); // load the navigation xml DataRow [] drs = ds. tables [0]. select ("url = '" + CurrentPageUrl + "'"); // search for the current node int rowIndex = drs Based on the url. count (); // considering that the parent node may have the same url, we plan to get the string url = ""; if (rowIndex> 0) at the bottom of the node) {rowIndex = rowIndex-1; url = urlLink (ds. tables [0], drs [rowIndex] ["siteMapNode_Id_0"]. toString () + string. format ("{0}", drs [rowIndex] ["title"]);} return url ;}/// /// Recursion of the parent node of the current node ///Private static string urlLink (DataTable dt, string parentid) {string Html = ""; if (! String. isNullOrEmpty (parentid) {DataRow [] drs = dt. select ("siteMapNode_Id =" + parentid); if (drs. count ()> 0) {string url = drs [0] ["url"]. toString (); if (! String. isNullOrEmpty (url) {url = string. format ("href = '{0}'", url);} Html = string. format ("{1}>", url, drs [0] ["title"]); Html = urlLink (dt, drs [0] ["siteMapNode_Id_0"]. toString () + Html ;}} return Html ;}3. html page call
4. Final Results