Use Web. sitemap on ASP. NET MVC

Source: Internet
Author: User

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

Related Article

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.