Site map ASP. net mvc SiteMap provider for ASP. NET MVC

Source: Internet
Author: User

Developers who have used traditional asp.net forms know that asp.net provides a sitemap function to conveniently generate navigation menus or navigation links. You only need to configure the function to manage navigation links. For the configuration and use of site map, the mainstream asp.net textbooks have typical examples, such as how to configure sitemap in xml format when using site map datasource. Asp.net mvc, as a new asp.net model, does not seem to have a built-in sitemap implementation function. Of course, in theory, we can still write hard-coded relative paths to nodes, but after all, it cannot be well combined with the controller and action in mvc. After all, when sitemap came out, there was no mvc yet. However, as a. net Framework, it has always provided excellent scalability. The classic policy mode is also applied to sitemap applications, and you only need to implement your own provider.

Recently, I saw a sitemap provider developed for asp.net on codeplex. It not only included the source code, but also showed how to apply it to the instance. The MvcMusicStore, an entry-level mvc application used by the instance, is indeed very popular. You have to install vs2010 to run it. After some simple research, I finally got some eyebrows and the effect was satisfactory. The specific implementation method is as follows:

Step 1:

To begin.

Step 2:

Configure the sitemap node in the web. config file. The configuration method is described in detail on the official website. It mainly provides a provider and you can set the attributes. There are many options available, but you can omit a lot of them.

SiteMap Configuration

<SiteMap defaultProvider = "MvcSiteMapProvider" enabled = "true">
<Providers>
<Clear/>
<Add name = "MvcSiteMapProvider"
Type = "MvcSiteMapProvider. DefaultSiteMapProvider, MvcSiteMapProvider"
SiteMapFile = "~ /Web. Sitemap"
SecurityTrimmingEnabled = "true"
CacheDuration = "5"
EnableLocalization = "true"
ScanAssembliesForSiteMapNodes = "true"
SkipAssemblyScanOn = ""
AttributesToIgnore = "bling, visibility"
NodeKeyGenerator = "MvcSiteMapProvider. DefaultNodeKeyGenerator, MvcSiteMapProvider"
ControllerTypeResolver = "MvcSiteMapProvider. DefaultControllerTypeResolver, MvcSiteMapProvider"
ActionMethodParameterResolver = "MvcSiteMapProvider. DefaultActionMethodParameterResolver, MvcSiteMapProvider"
AclModule = "MvcSiteMapProvider. DefaultAclModule, MvcSiteMapProvider"
SiteMapNodeUrlResolver = "MvcSiteMapProvider. DefaultSiteMapNodeUrlResolver, MvcSiteMapProvider"
SiteMapNodeVisibilityProvider = "MvcSiteMapProvider. DefaultSiteMapNodeVisibilityProvider, MvcSiteMapProvider"
SiteMapProviderEventHandler = "MvcSiteMapProvider. DefaultSiteMapProviderEventHandler, MvcSiteMapProvider"/>
</Providers>
</SiteMap>

Step 3:

Create a sitemap file. The method for creating a file is the same as the traditional method. You only need to change the content. The file name must be the same as that configured in web. config. Here is Web. sitemap. For typical xml cascading configuration methods, you must understand the meaning and methods of each attribute, such as title, controll, and action. The official website provides detailed configuration instructions.

Sitemap File

<? Xml version = "1.0" encoding = "UTF-8"?>
<MvcSiteMap xmlns = "http://mvcsitemap.codeplex.com/schemas/MvcSiteMap-File-2.0" enableLocalization = "true">
<MvcSiteMapNode title = "Homepage" controller = "Home" action = "Index" changeFrequency = "Always" updatePriority = "Normal">
<MvcSiteMapNode title = "About" controller = "Home" action = "About"/>
<MvcSiteMapNode title = "LogOn" controller = "Account" action = "LogOn"/>
<MvcSiteMapNode title = "Register" controller = "Account" action = "Register"/>
<MvcSiteMapNode title = "Product" controller = "Product" action = "Index">
<MvcSiteMapNode title = "product details" dynamicNodeProvider = "MvcSiteMapDemp. ProductDetailsDynamicNodeProvider, MvcSiteMapDemp"/>
</MvcSiteMapNode>
</MvcSiteMapNode>
</MvcSiteMap>

Step 4:

After that, the configuration is complete. Next we will discuss how to apply it. The mvc sitemap provider provides the called api and provides the display method by extending htmlhelper. The main methods are as follows (reference the description on the official website)

  • Html. MvcSiteMap (). Menu ()-Can be used to generate a menu
  • Html. MvcSiteMap (). SiteMap ()-Can be used to generate a list of all pages in your sitemap
  • Html. MvcSiteMap (). SiteMapPath ()-Can be used to generate a so-called "breadcrumb trail"
  • Html. MvcSiteMap (). SiteMapTitle ()-Can be used to render the current SiteMap node's title

    After completing the preceding steps, you can obtain navigation menus in a format similar to the following:

    Home> Products> product 2

    Of course, the general page can be configured in this simple way. If dynamic display is involved, such as the product detail node in this example, You need to implement your own node generation method, that is, to implement your own dynamicNodeProvider. The method for implementing custom NodeProvider is as follows:

    DynamicNodeProvider

    Public class ProductDetailsDynamicNodeProvider: DynamicNodeProviderBase
    {
    Public override IEnumerable <DynamicNode> GetDynamicNodeCollection ()
    {
    DynamicNode node1 = new DynamicNode ();
    Node1.Action = "ProductDetails ";
    Node1.Controller = "Product ";
    Node1.Title = "Product 1 ";
    Node1.RouteValues. Add ("name", "Product 1 ");

    DynamicNode node2 = new DynamicNode ();
    Node2.Action = "ProductDetails ";
    Node2.Controller = "Product ";
    Node2.Title = "product 2 ";
    Node2.RouteValues. Add ("name", "product 2 ");

    Return new DynamicNode [] {node1, node2 };
    }
    }

    The above is just an extension point. mvc sitemap provider provides multiple extension points to make the configuration more flexible. In terms of display, if you need to change the display link mode, you need to create DisplayTemplates in the shared folder of Views and place the Custom template in it. The complete template can be downloaded from the official website.

    Only the mvc sitemap provider is studied. More advanced applications need to be learned and supplemented.

  • Download instance code

    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.