2

I have a link in my url-list which includes dynamic ids, i.e.

/controller/action/id

Currently it's not needed to have for each of my pages an own breadcrumb for example, better would be to jump back to the index action breadcrumb or to simple set a parent resolving url.

Is there a simple solution or any tips how to solve this problem?

Thank you in advance

NightOwl888
  • 55,572
  • 24
  • 139
  • 212
Andy
  • 75
  • 5

1 Answers1

3

There are 2 ways to use an "id" or any other route value with MvcSiteMapProvider.

1. Create a node for each "id" (usually this is done with a DynamicNodeProviderBase implementation). This gives you a 1-to-1 relationship between the "id" value and the node. Use this method if you have < 10,000 nodes and you want them all indexed by search engines.

<mvcSiteMapNode title="Product 1" controller="Product" action="Details" id="1"/>
<mvcSiteMapNode title="Product 2" controller="Product" action="Details" id="2"/>
<mvcSiteMapNode title="Product 3" controller="Product" action="Details" id="3"/>

2. Create a single node to match all "id" values by setting preservedRouteParameters="id" on the node. This will give you a 1-to-1 relastionship between the route parameter name "id" (the value doesn't matter) and the node. This only works for the breadcrumb trail; for the Menu, SiteMap, and /sitemap.xml endpoints, you will need to use a visibility provider as well as a SiteMapTitle attribute to fix the display of the UI. Use this method for administration pages that edit data and will never be seen by search engines.

<mvcSiteMapNode title="Product 1" controller="Product" action="Details" preservedRouteParameters="id"/>

Note that you can also combine both techniques on the same node if you have multiple parameters. If, for example you have a user specific "userId" parameter that search engines will never need to know about, you can reduce the number of nodes (normally you would need to provide a node for [all "id" values] X [all "userId" values] - that is, the total node count would equal the product of all of the potential value combinations) in the SiteMap by always matching the "userId", but still index all of the products.

<mvcSiteMapNode title="Product 1" controller="Product" action="Details" id="1" preservedRouteParameters="userId"/>
<mvcSiteMapNode title="Product 2" controller="Product" action="Details" id="2" preservedRouteParameters="userId"/>
<mvcSiteMapNode title="Product 3" controller="Product" action="Details" id="3" preservedRouteParameters="userId"/>

Note also that you don't have to use XML to use these techniques - they also work when declaring nodes in other ways.

There is a complete article that describes each of these techniques with downloadable working samples on my blog: How to Make MvcSiteMapProvider Remember a User's Position.

NightOwl888
  • 55,572
  • 24
  • 139
  • 212
  • Thank you for your fast answer NightOwl888. I want to use the second way, because for the first way i'm having to many nodes and to hold them all the time, needs a huge amount of memory. Currently i'm using an external DI container with Unity. When i set the preservedRouteParameters to "id" then the first "Details" page is working with breadcrumbs, i.e. /details/452. When i go to another page i.e. /details/453 then i don't have any breadcrumbs anymore. I think it's a problem of caching. As far is i was reading on your page, "How to Make..." should cache automatically be turned off. – Andy Feb 05 '14 at 10:20
  • If using preservedRouteParameters="parameterName", you don't have to mess with caching because it will automatically be disabled in this case. You only need to disable resolved URL caching if you want to manually mimic what preservedRouteParameters does by using the static SiteMaps object within the action method to set the RouteValues. – NightOwl888 Feb 05 '14 at 10:42
  • I tried it with your example, it's working fine there. But in my project it isn't working. Also when i disable caching with cacheResolvedUrl="0" it doesn't work. Only when i release the whole cache it's loading my data. I tried also with the internal DI, but it's the same problem. Do you have any more idea what i can try? – Andy Feb 05 '14 at 17:09
  • Thank you NightOwl888, the solution was finally to add also the culture to the preservedRouteParameters. It's working fine now. – Andy Feb 11 '14 at 13:46