Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
649 views
in Technique[技术] by (71.8m points)

asp.net mvc - MvcSitemapProvider - Add value to ignore in url resolver

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...