1

I setup MVC breadcumbs for my website using MvcSiteMapProvider (Nuget package MvcSiteMapProvider.MVC5 (version 4.6.22)).

It works fine.

Then I want to update Url of Sitemap dynamically like:

SiteMaps.Current.CurrentNode.Url = Url.Action("Index");

Then I got this error:

SiteMapNode is readonly, property 'Url' cannot be modified

Note that I am still able to update Title:

SiteMaps.Current.CurrentNode.Title = "/Index";

Any idea?

Minh Nguyen
  • 2,106
  • 1
  • 28
  • 34

1 Answers1

1

The SiteMap is a statically cached object that is shared between all users. Technically, all of the properties are read-only at runtime. However, some of the properties (such as Title) are request-cached so you can safely update them at runtime without affecting other users.

The Url property is a special property that dynamically builds the URL through the MVC UrlHelper class (which is directly driven from your routes). It makes no sense to set it to Url.Action("Index") because that is effectively what it does just by itself (unless you are using a dynamic node provider or custom ISiteMapNodeProvider - those are startup extension points where you load the node configuration, so the properties are read-write).

You just need to set the correct controller and action in your node configuration (which could be XML, attribute based, or code based) and the URL will resolve on its own.

XML Example

<mvcSiteMapNode title="Projects" controller="Project" action="Index"/>

NOTE: You need to account for all route values in the request, either by adding them as another attribute myId="123" or by using preservedRouteParameters="myId" (which tells it to include the myId from the current request when building the URL). See this article for a detailed description of using these options.

NOTE: Setting a URL in the SiteMap configuration effectively overrides MVC support for that node. So, you shouldn't set a URL at all unless it is a non-MVC URL.

Community
  • 1
  • 1
NightOwl888
  • 55,572
  • 24
  • 139
  • 212
  • My above code is just made simple to illustrate the issue. In fact I want to modify my Parent SiteMapNode Url to meet my requirements as the default URL is having incorrect Id. I was still able to edit Node URL in previous version of MvcSiteMap Provider though. So now I need to user different approach? – Minh Nguyen Apr 05 '16 at 09:31
  • The previous version of `MvcSiteMapProvider` was fundamentally broken in that it allowed this. When you updated the URL it was being written to the global cache, which affected *all users* of the web site. You should instead aim to load your properties at startup. If the URL is wrong, it is an indication that either your node configuration is wrong or your MVC routing configuration is wrong. See the article I linked to in my `NOTE` - it explains everything you need to do to make it work and has demos as well. – NightOwl888 Apr 05 '16 at 09:45
  • Do you now if there is a way to pass in a parameter to Dynamic Node Provider? – Minh Nguyen Apr 05 '16 at 11:34
  • Dynamic node providers are used to *load* the `SiteMap` (into the global cache). So you would not pass per-request values there. I suggest you ask a different question and present the problem you are trying to solve. There is probably a solution to what you are trying to achieve without pushing a URL into a node, but I can't explain how to solve your issue if I don't know what it is. – NightOwl888 Apr 05 '16 at 11:49