How can I create sitemap nodes programmatically?

Nov 5, 2010 at 12:21 PM

I want to use MvcSiteMap to define a sitemap of my controllers and actions, to let me generate breadcrumbs and menus.

I have tried using the below decorators to add nodes programmatically, but unfortunately it will not make my tree like I want. It seems the structure becomes flat?

[MvcSiteMapNodeAttribute(Title = "Home"]
[MvcSiteMapNodeAttribute(Title = "Services", ParentKey = "Home")]
[MvcSiteMapNodeAttribute(Title = "Service detail", ParentKey = "Services")]
[MvcSiteMapNodeAttribute(Title = "Edit", ParentKey = "Service detail")]

How can I decorate my actions to make sure the child/parent relations are made how I want?

[HandleError]
public class HomeController : Controller
{
    // Home
    public ActionResult Index ()
    {
        return View();
    }
}

[HandleError]
public class ServiceController : Controller
{
    // Home > Services
    public ActionResult Index ()
    {
        return View();
    }

    // Home > Services > Service detail
    public ActionResult Details (int id)
    {
        return View();
    }

    // Home > Services > Service detail > Edit
    public ActionResult Edit (int id)
    {
        return View();
    }
}

Coordinator
Nov 8, 2010 at 6:39 AM

This should work. Key is to specify the key for each node manually as MvcSiteMapProvider will generate a more "funky" key if none is provided.

 

[HandleError]
public class HomeController : Controller
{
    // Home
    [MvcSiteMapNodeAttribute(Title = "Home", Key = "Home"]
public ActionResult Index () { return View(); } } [HandleError] public class ServiceController : Controller { // Home > Services [MvcSiteMapNodeAttribute(Title = "Services", Key="Services", ParentKey = "Home")]
public ActionResult Index () { return View(); } // Home > Services > Service detail [MvcSiteMapNodeAttribute(Title = "Service detail", Key = "Services_Detail" ParentKey = "Services")]

public ActionResult Details (int id) { return View(); } // Home > Services > Service detail > Edit [MvcSiteMapNodeAttribute(Title = "Edit", ParentKey = "Services_Detail")]
public ActionResult Edit (int id) { return View(); } }
Nov 8, 2010 at 8:52 AM

Hi,

doing this works fine until i call Html.MvcSiteMap().SiteMapPath() ..

An unhandled exception of type 'System.StackOverflowException' occurred in mscorlib.dll
System.StackOverflowException was unhandled

Any ideas?

Nov 8, 2010 at 9:58 AM
Edited Nov 8, 2010 at 10:00 AM

Seems like the problem appears if i use a ParentKey defined in another controller?

I have the following:

public class MainController : Controller
{
    [Authorize]
    [MvcSiteMapNode(Title = "Home", Key = "Home")]
    public ActionResult Index()
    {
        return View();
    }
}

and

public class ServiceController : Controller
{
    [Authorize]
    [MvcSiteMapNode(Title = "Services", Key = "Service", ParentKey = "Home")]
    public ActionResult Index()
    {
        Sl _service = new Sl();
        return View();
    }

    [Authorize]
    [MvcSiteMapNode(Title = "Detail", Key = "Service_Details", ParentKey = "Service")]
    public ActionResult Details(int id)
    {
        return View();
    }
}

When I remove the decorator in MainController, and remove the ParentKey=Home from my ServiceController, everything works fine...

 

Coordinator
Nov 10, 2010 at 5:51 AM

This should work. Are you seeing errors? Can you post stacktrace, innerexception, ...?