2

Mvc 5, .Net 4.5

I implement MvcSiteMapNodeAttribute as follows:

[MvcSiteMapNode(Title="Running Events", ParentKey="Events", Key="RunningEvents")]
public ActionResult RunningEvents()
{
    return View();
}

I need to access this page from multiple locations and keep the breadcrumbs in tack (i.e. from the correct calling method). However the ParentKey dictates where the call comes from and thus set the ParentNode based on it. This is not ideal as I want the calling ActionResult to be the parent and not "hard coded" as with the ParentKey solution. The ParentKey is also not editable at runtime nor the ParentNode. The only way around this at the moment is to duplicate the ActionResult with different signatures and give it the same Title which is also not ideal.

I've read up on mvc routing, DynamicNodeProvider, route mapping, etc but cannot find a way to make this work? I'm also not very familiar with mvc so would appreciate some guidance.

Thanks

1 Answer 1

1

See the Multiple Navigation Paths to a Single Page documentation.

You can use the same controller action in multiple places. However, you must always provide a unique set of route values (which usually means each URL should be unique).

The most natural way to do this is to design your URLs with the parent category.

routes.MapRoute(
    name: "Category1RunningEvents",
    url: "Category1/RunningEvents",
    defaults: new { controller = "Events", action = "RunningEvents", category="Category1" }
);

routes.MapRoute(
    name: "Category2RunningEvents",
    url: "Category2/RunningEvents",
    defaults: new { controller = "Events", action = "RunningEvents", category="Category2" }
);

routes.MapRoute(
    name: "Default",
    url: "{controller}/{action}/{id}",
    defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);

And then differentiate the route matching by the category field. You can use multiple MvcSiteMapNode attributes on the same action, each with different parent key in the SiteMap.

[MvcSiteMapNode(Title="Category 1 Events", ParentKey="Events", Key="RunningEvents", Attributes = @"{ ""category"": ""Category1"" }")]
[MvcSiteMapNode(Title="Category 2 Events", ParentKey="Category2", Key="Category2RunningEvents", Attributes = @"{ ""category"": ""Category2"" }")]
public ActionResult RunningEvents()
{
    return View();
}

Of course, this isn't the only way to configure the routing but it should clear up the concept. The only limitation is that you must use a unique set of route values for the match, each which corresponds to a node. However, there can be multiple nodes that represent the same controller action, each with a different parent node.

Also see this answer.

1
  • Thanks for the response NightOwl.
    – Jean Roux
    Jul 4, 2016 at 8:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.